Backends#

What is a backend?#

Backends are used for displaying Matplotlib figures (seeIntroduction to Figures),on the screen, or for writing to files. A lot of documentation on the websiteand in the mailing lists refers to the "backend" and many new users areconfused by this term. Matplotlib targets many different use cases and outputformats. Some people use Matplotlib interactively from the Python shell andhave plotting windows pop up when they type commands. Some people runJupyter notebooks and draw inline plots for quick dataanalysis. Others embed Matplotlib into graphical user interfaces like PyQt orPyGObject to build rich applications. Some people use Matplotlib in batchscripts to generate postscript images from numerical simulations, and stillothers run web application servers to dynamically serve up graphs.

To support all of these use cases, Matplotlib can target differentoutputs, 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 inPyQt/PySide, PyGObject, Tkinter, wxPython, or macOS/Cocoa); also referred toas "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:

Below is a more detailed description.

If there is more than one configuration present, the last one from thelist takes precedence; e.g. callingmatplotlib.use() will overridethe setting in yourmatplotlibrc.

Without a backend explicitly set, Matplotlib automatically detects a usablebackend based on what is available on your system and on whether a GUI eventloop is already running. The first usable backend in the following list isselected: MacOSX, QtAgg, GTK4Agg, Gtk3Agg, TkAgg, WxAgg, Agg. The last, Agg,is a non-interactive backend that can only write to files. It is used onLinux, if Matplotlib cannot connect to either an X display or a Waylanddisplay.

Here is a detailed description of the configuration methods:

  1. SettingrcParams["backend"] in yourmatplotlibrc file:

    backend:qtagg# use pyqt with antigrain (agg) rendering

    See alsoCustomizing Matplotlib with style sheets and rcParams.

  2. Setting theMPLBACKEND environment variable:

    You can set the environment variable either for your current shell or fora single script.

    On Unix:

    >exportMPLBACKEND=qtagg>pythonsimple_plot.py>MPLBACKEND=qtaggpythonsimple_plot.py

    On Windows, only the former is possible:

    >setMPLBACKEND=qtagg>pythonsimple_plot.py

    Setting this environment variable will override thebackend parameterinanymatplotlibrc, even if there is amatplotlibrc inyour current working directory. Therefore, settingMPLBACKENDglobally, e.g. in your.bashrc or.profile, is discouragedas it might lead to counter-intuitive behavior.

  3. If your script depends on a specific backend you can use the functionmatplotlib.use():

    importmatplotlibmatplotlib.use('qtagg')

    This should be done before any figure is created, otherwise Matplotlib mayfail to switch the backend and raise an ImportError.

    Usinguse will require changes in your code if users want touse a different backend. Therefore, you should avoid explicitly callinguse unless absolutely necessary.

The builtin backends#

By default, Matplotlib should automatically select a default backend whichallows both interactive work and plotting from scripts, with output to thescreen and/or to a file, so at least initially, you will not need to worryabout the backend. The most common exception is if your Python distributioncomes withouttkinter and you have no other GUI toolkit installed.This happens with certain Linux distributions, where you need to install aLinux package namedpython-tk (or similar).

If, however, you want to write graphical user interfaces, or a webapplication server(Embed in a web application server (Flask)), or need abetter understanding of what is going on, read on. To make things easilymore customizable for graphical user interfaces, Matplotlib separatesthe concept of the renderer (the thing that actually does the drawing)from the canvas (the place where the drawing goes). The canonicalrenderer for user interfaces isAgg which uses theAnti-GrainGeometry C++ library to make a raster (pixel) image of the figure; itis used by theQtAgg,GTK4Agg,GTK3Agg,wxAgg,TkAgg, andmacosx backends. An alternative renderer is based on the Cairo library,used byQtCairo, etc.

For the rendering engines, users can also distinguish betweenvector orraster renderers. Vectorgraphics languages issue drawing commands like "draw a line from thispoint to this point" and hence are scale free. Raster backendsgenerate a pixel representation of the line whose accuracy depends on aDPI setting.

Static backends#

Here is a summary of the Matplotlib renderers (there is an eponymousbackend for each; these arenon-interactive backends, capable ofwriting to a file):

Renderer

Filetypes

Description

AGG

png

raster graphics -- high quality images using theAnti-Grain Geometry engine.

PDF

pdf

vector graphics --Portable Document Format output.

PS

ps, eps

vector graphics --PostScript output.

SVG

svg

vector graphics --Scalable Vector Graphics output.

PGF

pgf, pdf

vector graphics -- using thepgf package.

Cairo

png, ps,pdf, svg

raster orvector graphics -- using theCairo library(requirespycairo orcairocffi).

To save plots using the non-interactive backends, use thematplotlib.pyplot.savefig('filename') method.

Interactive backends#

These are the user interfaces and renderer combinations supported;these areinteractive backends, capable of displaying to the screenand using appropriate renderers from the table above to write toa file:

Backend

Description

QtAgg

Agg rendering in aQt canvas (requiresPyQt orQt for Python,a.k.a. PySide). This backend can be activated in IPython with%matplotlibqt. The Qt binding can be selected via theQT_API environment variable; seeQt Bindings formore details.

ipympl

Agg rendering embedded in a Jupyter widget (requiresipympl).This backend can be enabled in a Jupyter notebook with%matplotlibipympl or%matplotlibwidget. Works withJupyterlab andnotebook>=7.

GTK3Agg

Agg rendering to aGTK 3.x canvas (requiresPyGObject andpycairo). This backend can be activated in IPython with%matplotlibgtk3.

GTK4Agg

Agg rendering to aGTK 4.x canvas (requiresPyGObject andpycairo). This backend can be activated in IPython with%matplotlibgtk4.

macosx

Agg rendering into a Cocoa canvas in macOS. This backend can beactivated in IPython with%matplotlibosx.

TkAgg

Agg rendering to aTk canvas (requiresTkInter). Thisbackend can be activated in IPython with%matplotlibtk.

nbAgg

Embed an interactive figure in a Jupyter classic notebook. Thisbackend can be enabled in Jupyter notebooks via%matplotlibnotebook or%matplotlibnbagg. Works withJupyternotebook<7 andnbclassic.

WebAgg

Onshow() will start a tornado server with an interactivefigure.

GTK3Cairo

Cairo rendering to aGTK 3.x canvas (requiresPyGObject andpycairo).

GTK4Cairo

Cairo rendering to aGTK 4.x canvas (requiresPyGObject andpycairo).

wxAgg

Agg rendering to awxWidgets canvas (requireswxPython 4).This backend can be activated in IPython with%matplotlibwx.

Note

The names of builtin backends are case-insensitive; e.g., 'QtAgg' and'qtagg' are equivalent.

ipympl#

The ipympl backend is in a separate package that must be explicitly installedif you wish to use it, for example:

pipinstallipympl

or

condainstallipympl-cconda-forge

Seeinstalling ipympl for more details.

Using non-builtin backends#

More generally, any importable backend can be selected by using any of themethods above. Ifname.of.the.backend is the module containing thebackend, usemodule://name.of.the.backend as the backend name, e.g.matplotlib.use('module://name.of.the.backend').

Information for backend implementers is available atWriting 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 (seeNotebooks and IDEs),please consult their documentation for debugging figures not working in theirenvironments.

If you are using one of Matplotlib's graphics backends (seeStandalone scripts and interactive use), make sure you know whichone is being used:

importmatplotlibprint(matplotlib.get_backend())

Try a simple plot to see if the GUI opens:

importmatplotlibimportmatplotlib.pyplotaspltprint(matplotlib.get_backend())plt.plot((1,4,6))plt.show()

If it does not, you perhaps have an installation problem. A good step at thispoint is to ensure that your GUI toolkit is installed properly, takingMatplotlib out of the testing. Almost all GUI toolkits have a small testprogram that can be run to test basic functionality. If this test fails, try re-installing.

QtAgg, QtCairo, Qt5Agg, and Qt5Cairo#

TestPyQt6 (if you havePyQt5,PySide2 orPySide6 installedrather thanPyQt6, just change the import accordingly):

python3-c"from PyQt6.QtWidgets import *; app = QApplication([]); win = QMainWindow(); win.show(); app.exec()"

TkAgg and TkCairo#

Testtkinter:

python3-c"from tkinter import Tk; Tk().mainloop()"

GTK3Agg, GTK4Agg, GTK3Cairo, GTK4Cairo#

TestGtk:

python3-c"from gi.repository import Gtk; win = Gtk.Window(); win.connect('destroy', Gtk.main_quit); win.show(); Gtk.main()"

wxAgg and wxCairo#

Testwx:

python3-c"import wx; app = wx.App(); frame = wx.Frame(None); frame.Show(); app.MainLoop()"

If the test works for your desired backend but you still cannot get Matplotlib to display a figure, then contact us (seeGet help).