Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.

things to do

There is a Python interface in libsixel, so let's use it.



How to do

You can convert the byte string to Sixel data, but it seems that you have to use the C API to do that, so you can easily realize it by discarding the runtime efficiency.

from libsixel.encoder import Encoder as SixelEncoder
from tempfile import NamedTemporaryFile

def sixeldraw(width=None,height=None):
    with NamedTemporaryFile(prefix="sixel-") as fd:
        encoder = SixelEncoder()
        if width : encoder.setopt(SIXEL_OPTFLAG_WIDTH, width)
        if height: encoder.setopt(SIXEL_OPTFLAG_HEIGHT,height)

if __name__=="__main__":
    import matplotlib
    import pylab as pl
    import numpy as np
    x = np.linspace(0,1)
    y = x**2

Yes. Create a file temporarily and save the graph image. Output the image file to standard output in Sixel format using libsixel's python interface. If you use a terminal that supports Sixel, you can get an image.

It is a cut-out implementation.

Recommended Posts

Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
How to use the __call__ method in a Python class
[Mac] A super-easy way to execute system commands in Python and output the results
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
[Introduction to Python] How to use the in operator in a for statement?
How to use is and == in Python
Connect to postgreSQL from Python and use stored procedures in a loop.
How to use the C library in Python
Output in the form of a python array
[Python] How to draw a histogram in Matplotlib
Let's use Python to represent the frequency of binary data contained in a data frame in a single bar graph.
How to input a character string in Python and output it as it is or in the opposite direction.
How to count the number of elements in Django and output to a template
I tried to graph the packages installed in Python
[Python] How to draw a line graph with Matplotlib
[Python] How to use the graph creation library Altair
Convenient to use matplotlib subplots in a for statement
Use communicate () when receiving output in a Python subprocess
How to use the model learned in Lobe in Python
Output a binary dump in binary and revert to a binary file
[Python] How to output the list values in order
I want to use the R dataset in python
Run the output code with tkinter, saying "A, pretending to be B" in python
[Python] Change the text color and background color of a specific keyword in print output
I created a stacked bar graph with matplotlib in Python and added a data label
It is easy to execute SQL with Python and output the result in Excel
Output "Draw ferns programmatically" to the drawing process in Python
[python] How to use the library Matplotlib for drawing graphs
Sort and output the elements in the list as elements and multiples in Python.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
A standard way to develop and distribute packages in Python
Build a Python environment and transfer data to the server
Comparison of how to use higher-order functions in Python 2 and 3
How to get the last (last) value in a list in Python
Graph time series data in Python using pandas and matplotlib
Just try to receive a webhook in ngrok and python
Graph the Poisson distribution and the Poisson cumulative distribution in Python and Java, respectively.
Recursively get the Excel list in a specific folder with python and write it to Excel.
I also tried to imitate the function monad and State monad with a generator in Python
[Python] When you want to import and use your own package in the upper directory
An easy way to view the time taken in Python and a smarter way to improve it
[Python / matplotlib] Understand and use FuncAnimation
How to use SQLite in Python
In the python command python points to python3.8
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
Export and output files in Python
The first step in Python Matplotlib
Output the key list included in S3 Bucket to a file
Determine the date and time format in Python and convert to Unixtime
How to determine the existence of a selenium element in Python
Hit the echo command in the Mac terminal to output Hello World
[Introduction to Python] How to output a character string in a Print statement
How to use functions in separate files Perl and Python versions
Output the contents of ~ .xlsx in the folder to HTML with Python
Read the standard output of a subprocess line by line in Python
How to use the asterisk (*) in Python. Maybe this is all? ..
How to check the memory size of a dictionary in Python
Goodbye to the command prompt in WSL, VSCode and Windows Terminal
I made a command to display a colorful calendar in the terminal