show matplotlib plots directly in ipython in your terminal
Project description
matplotlib-backend-terminal
This python module allows you to use your terminal to show inline plots generated by python's matplotlib using the chafa image viewer.
The module is a modified version of the kitty backend and is also based on this sixel backend.
You will need a terminal that has support for displaying graphics and chafa installed.
To install either run
$ pip install --user matplotlib-backend-terminal
or clone this repo and run
$ cd matplotlib-backend-terminal && pip install .
Configure matplotlib to use the module by either setting the
environment variable MPLBACKEND to module://matplotlib-backend-terminal
or by initializing matplotlib as follows.
import matplotlib
matplotlib.use('module://matplotlib-backend-terminal')
import matplotlib.pyplot as plt
Any flags required by chafa can be passed to the backend by setting the MPLBACKEND_CHAFA_OPTS environment variable. For example, to use the --passthrough tmux flag, you can run
$ export MPLBACKEND_CHAFA_OPTS="--passthrough tmux"
See the chafa documentation for more information on available flags and options. Once chafa works in your terminal setup it should work with this backend.
To enable transparency in the terminal, you can set the MPLBACKEND_TRANSPARENT environment variable to 1. This will make the background of the plot transparent, allowing the terminal background to show through. Note that this will not work with all output formats.
If you've installed this module correctly, you can now use the following sample code to draw a plot in your terminal.
import matplotlib
matplotlib.use('module://matplotlib-backend-terminal')
import numpy as np
import matplotlib.pyplot as plt
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>
Example output using Ghostty terminal.
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.
Internally, this backend is somewhat based on matplotlib's
IPython support: it's a hybrid of image and GUI backend types.
It works by using matplotlib's Agg backend to render the
plot, and then calls chafa to display in the terminal. This means that plotting works as
expected, but the image drawn to your terminal isn't
interactive and animations aren't supported.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matplotlib_backend_terminal-0.1.0.tar.gz.
File metadata
- Download URL: matplotlib_backend_terminal-0.1.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e010e28dfaef087a7ab3692f6721645bd77dd64ce07631044aeb75aafd00f62
|
|
| MD5 |
5758906fc9c344989bb0515456cf126b
|
|
| BLAKE2b-256 |
26a0eeeab447ca22e5c29a4209bafd49e711494bacec9c0a005d9f54e2caa21a
|
File details
Details for the file matplotlib_backend_terminal-0.1.0-py3-none-any.whl.
File metadata
- Download URL: matplotlib_backend_terminal-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d061baef581b7e87327b84a7028b70b77115f0bcbac6db8bf519c092209eb45
|
|
| MD5 |
c5b0c2da219a1dc85f48c7244e9adcbb
|
|
| BLAKE2b-256 |
133c5d2b8cf9bbe289b15f1956cbe256575e02874d4ce2d5395218a02f52861f
|