Backends#
What is a backend?#
A lot of documentation on the website and in the mailing lists refers to the "backend" and many new users are confused by this term. Matplotlib targets many different use cases and output formats. Some people use Matplotlib interactively from the Python shell and have plotting windows pop up when they type commands. Some people run Jupyter notebooks and draw inline plots for quick data analysis. Others embed Matplotlib into graphical user interfaces like PyQt or PyGObject to build rich applications. Some people use Matplotlib in batch scripts to generate postscript images from numerical simulations, and still others run web application servers to dynamically serve up graphs.
To support all of these use cases, Matplotlib can target different outputs, and each of these capabilities is called a backend; the "frontend" is the user facing code, i.e., the plotting code, whereas the "backend" does all the hard work behind-the-scenes to make the figure. There are two types of backends: user interface backends (for use in PyQt/PySide, PyGObject, Tkinter, wxPython, or macOS/Cocoa); also referred to as "interactive backends") and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as "non-interactive backends").
Selecting a backend#
There are three ways to configure your backend:
The
rcParams["backend"]
parameter in yourmatplotlibrc
fileThe
MPLBACKEND
environment variableThe function
matplotlib.use()
Below is a more detailed description.
If there is more than one configuration present, the last one from the
list takes precedence; e.g. calling matplotlib.use()
will override
the setting in your matplotlibrc
.
Without a backend explicitly set, Matplotlib automatically detects a usable backend based on what is available on your system and on whether a GUI event loop is already running. The first usable backend in the following list is selected: MacOSX, QtAgg, GTK4Agg, Gtk3Agg, TkAgg, WxAgg, Agg. The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.
Here is a detailed description of the configuration methods:
Setting
rcParams["backend"]
in yourmatplotlibrc
file:backend : qtagg # use pyqt with antigrain (agg) rendering
See also Customizing Matplotlib with style sheets and rcParams.
Setting the
MPLBACKEND
environment variable:You can set the environment variable either for your current shell or for a single script.
On Unix:
> export MPLBACKEND=qtagg > python simple_plot.py > MPLBACKEND=qtagg python simple_plot.py
On Windows, only the former is possible:
> set MPLBACKEND=qtagg > python simple_plot.py
Setting this environment variable will override the
backend
parameter in anymatplotlibrc
, even if there is amatplotlibrc
in your current working directory. Therefore, settingMPLBACKEND
globally, e.g. in your.bashrc
or.profile
, is discouraged as it might lead to counter-intuitive behavior.If your script depends on a specific backend you can use the function
matplotlib.use()
:import matplotlib matplotlib.use('qtagg')
This should be done before any figure is created, otherwise Matplotlib may fail to switch the backend and raise an ImportError.
Using
use
will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly callinguse
unless absolutely necessary.
The builtin backends#
By default, Matplotlib should automatically select a default backend which
allows both interactive work and plotting from scripts, with output to the
screen and/or to a file, so at least initially, you will not need to worry
about the backend. The most common exception is if your Python distribution
comes without tkinter
and you have no other GUI toolkit installed.
This happens on certain Linux distributions, where you need to install a
Linux package named python-tk
(or similar).
If, however, you want to write graphical user interfaces, or a web
application server
(Embedding in a web application server (Flask)), or need a
better understanding of what is going on, read on. To make things easily
more customizable for graphical user interfaces, Matplotlib separates
the concept of the renderer (the thing that actually does the drawing)
from the canvas (the place where the drawing goes). The canonical
renderer for user interfaces is Agg
which uses the Anti-Grain
Geometry C++ library to make a raster (pixel) image of the figure; it
is used by the QtAgg
, GTK4Agg
, GTK3Agg
, wxAgg
, TkAgg
, and
macosx
backends. An alternative renderer is based on the Cairo library,
used by QtCairo
, etc.
For the rendering engines, users can also distinguish between vector or raster renderers. Vector graphics languages issue drawing commands like "draw a line from this point to this point" and hence are scale free. Raster backends generate a pixel representation of the line whose accuracy depends on a DPI setting.
Here is a summary of the Matplotlib renderers (there is an eponymous backend for each; these are non-interactive backends, capable of writing to a file):
Renderer |
Filetypes |
Description |
---|---|---|
AGG |
png |
raster graphics -- high quality images using the Anti-Grain Geometry engine. |
vector graphics -- Portable Document Format output. |
||
PS |
ps, eps |
vector graphics -- PostScript output. |
SVG |
svg |
vector graphics -- Scalable Vector Graphics output. |
PGF |
pgf, pdf |
|
Cairo |
png, ps, pdf, svg |
raster or vector graphics -- using the Cairo library (requires pycairo or cairocffi). |
To save plots using the non-interactive backends, use the
matplotlib.pyplot.savefig('filename')
method.
These are the user interfaces and renderer combinations supported; these are interactive backends, capable of displaying to the screen and using appropriate renderers from the table above to write to a file:
Backend |
Description |
---|---|
QtAgg |
Agg rendering in a Qt canvas (requires PyQt or Qt for Python,
a.k.a. PySide). This backend can be activated in IPython with
|
ipympl |
Agg rendering embedded in a Jupyter widget (requires ipympl).
This backend can be enabled in a Jupyter notebook with
|
GTK3Agg |
Agg rendering to a GTK 3.x canvas (requires PyGObject and
pycairo). This backend can be activated in IPython with
|
GTK4Agg |
Agg rendering to a GTK 4.x canvas (requires PyGObject and
pycairo). This backend can be activated in IPython with
|
macosx |
Agg rendering into a Cocoa canvas in OSX. This backend can be
activated in IPython with |
TkAgg |
Agg rendering to a Tk canvas (requires TkInter). This
backend can be activated in IPython with |
nbAgg |
Embed an interactive figure in a Jupyter classic notebook. This
backend can be enabled in Jupyter notebooks via
|
WebAgg |
On |
GTK3Cairo |
Cairo rendering to a GTK 3.x canvas (requires PyGObject and pycairo). |
GTK4Cairo |
Cairo rendering to a GTK 4.x canvas (requires PyGObject and pycairo). |
wxAgg |
Agg rendering to a wxWidgets canvas (requires wxPython 4).
This backend can be activated in IPython with |
Note
The names of builtin backends case-insensitive; e.g., 'QtAgg' and 'qtagg' are equivalent.
ipympl#
The Jupyter widget ecosystem is moving too fast to support directly in Matplotlib. To install ipympl:
pip install ipympl
or
conda install ipympl -c conda-forge
See installing ipympl for more details.
How do I select the Qt implementation?#
The QtAgg and QtCairo backends support both Qt 5 and 6, as well as both Python bindings (PyQt or Qt for Python, a.k.a. PySide). If any binding has already been loaded, then it will be used for the Qt backend. Otherwise, the first available binding is used, in the order: PyQt6, PySide6, PyQt5, PySide2.
The QT_API
environment variable can be set to override the search
when nothing has already been loaded. It may be set to (case-insensitively)
PyQt6, PySide6, PyQt5, or PySide2 to pick the version and binding to use. If
the chosen implementation is unavailable, the Qt backend will fail to load
without attempting any other Qt implementations. See Qt Bindings for
more details.
Using non-builtin backends#
More generally, any importable backend can be selected by using any of the
methods above. If name.of.the.backend
is the module containing the
backend, use module://name.of.the.backend
as the backend name, e.g.
matplotlib.use('module://name.of.the.backend')
.