/matplotlib-backend-sixel

Primary LanguagePythonCreative Commons Zero v1.0 UniversalCC0-1.0

matplotlib-backend-sixel

This python module allows you to use your sixel-enabled terminal to show inline plots generated by python's matplotlib.

Install with

pip install git+https://github.com/vincentqb/matplotlib-backend-sixel.git

Use by either setting the environment variable MPLBACKEND to module://matplotlib-backend-sixel or by initializing matplotlib as in the example below

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

matplotlib.use("module://matplotlib-backend-sixel")

plt.style.use("dark_background")

t = np.linspace(0, 5, 200)
plt.fill_between(t, np.sin(t), np.cos(2 * t), alpha=0.5)
plt.fill_between(t, np.cos(t), np.sin(2 * t), alpha=0.5)

plt.show()
# <plot is shown>

The output with Gnome terminal compiled with SIXEL support looks as follows

Example output

If you set your matplotlib to interactive mode via matplotlib.pyplot.ion() or by running python as python -i, non-empty figures are drawn on construction where possible. This allows you to use pandas' plot() calls directly, without calling plt.show(), and still enables you to manually construct and plt.show().

If your matplotlib is in non-interactive mode, you can construct your figures as usual, and then call plt.show() to render them to your terminal. This works from both a repl and when running scripts.

This is forked from ctorney with patch from yimuchen. The module is a modified version of the kitty backend and is also based on this earlier sixel backend. Internally, this backend is somewhat based on matplotlib's IPython support, and is a hybrid of image and GUI backend types. It works by using matplotlib's Agg backend to render the plot, and then calls python-sixel to convert to sixel format and then cat the image to your terminal. This means that plotting works as expected, but the image drawn to your terminal isn't interactive and animations aren't supported.

Test