What is a backend?#
Backends are used for displaying Matplotlib figures (see Introduction to Figures), on the screen, or for writing to files. 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:
rcParams["backend"]parameter in your
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
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:
backend : qtagg # use pyqt with antigrain (agg) rendering
You can set the environment variable either for your current shell or for a single script.
> 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
backendparameter in any
matplotlibrc, even if there is a
matplotlibrcin your current working directory. Therefore, setting
MPLBACKENDglobally, e.g. in your
.profile, is discouraged as it might lead to counter-intuitive behavior.
If your script depends on a specific backend you can use the function
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.
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
tkinter and you have no other GUI toolkit installed.
This happens with 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
(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
macosx backends. An alternative renderer is based on the Cairo library,
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):
png, ps, pdf, svg
To save plots using the non-interactive backends, use the
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:
Agg rendering in a Qt canvas (requires PyQt or Qt for Python,
a.k.a. PySide). This backend can be activated in IPython with
Agg rendering embedded in a Jupyter widget (requires ipympl).
This backend can be enabled in a Jupyter notebook with
Agg rendering into a Cocoa canvas in OSX. This backend can be
activated in IPython with
Embed an interactive figure in a Jupyter classic notebook. This
backend can be enabled in Jupyter notebooks via
The names of builtin backends case-insensitive; e.g., 'QtAgg' and 'qtagg' are equivalent.
The Jupyter widget ecosystem is moving too fast to support directly in Matplotlib. To install ipympl:
pip install ipympl
conda install ipympl -c conda-forge
See installing ipympl 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
module://name.of.the.backend as the backend name, e.g.
Information for backend implementers is available at Writing a backend -- the pyplot interface.
Debugging the figure windows not showing#
Sometimes things do not work as expected, usually during an install.
If you are using a Notebook or integrated development environment (see Notebooks and IDEs), please consult their documentation for debugging figures not working in their environments.
If you are using one of Matplotlib's graphics backends (see Standalone scripts and interactive use), make sure you know which one is being used:
import matplotlib print(matplotlib.get_backend())
Try a simple plot to see if the GUI opens:
import matplotlib import matplotlib.pyplot as plt print(matplotlib.get_backend()) plt.plot((1, 4, 6)) plt.show()
If it does not, you perhaps have an installation problem. A good step at this point is to ensure that your GUI toolkit is installed properly, taking Matplotlib out of the testing. Almost all GUI toolkits have a small test program that can be run to test basic functionality. If this test fails, try re-installing.
QtAgg, QtCairo, Qt5Agg, and Qt5Cairo#
If you have
PyQt6 installed rather than
PyQt5, just change the import
python -c "from PyQt5.QtWidgets import *; app = QApplication(); win = QMainWindow(); win.show(); app.exec()"
TkAgg and TkCairo#
python3 -c "from tkinter import Tk; Tk().mainloop()"
GTK3Agg, GTK4Agg, GTK3Cairo, GTK4Cairo#
python3 -c "from gi.repository import Gtk; win = Gtk.Window(); win.connect('destroy', Gtk.main_quit); win.show(); Gtk.main()"
wxAgg and wxCairo#
import wx app = wx.App(False) # Create a new app, don't redirect stdout/stderr to a window. frame = wx.Frame(None, wx.ID_ANY, "Hello World") # A Frame is a top-level window. frame.Show(True) # Show the frame. app.MainLoop()
If the test works for your desired backend but you still cannot get Matplotlib to display a figure, then contact us (see Get help).