1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
.. redirect-from:: /users/explain/backends
.. _backends:
========
Backends
========
.. _what-is-a-backend:
What is a backend?
------------------
Backends are used for displaying Matplotlib figures (see :ref:`figure-intro`),
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
<https://jupyter.org>`_ 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 :rc:`backend` parameter in your :file:`matplotlibrc` file
- The :envvar:`MPLBACKEND` environment variable
- The function :func:`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 :func:`matplotlib.use()` will override
the setting in your :file:`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 :rc:`backend` in your :file:`matplotlibrc` file::
backend : qtagg # use pyqt with antigrain (agg) rendering
See also :ref:`customizing`.
#. Setting the :envvar:`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 *any* :file:`matplotlibrc`, even if there is a :file:`matplotlibrc` in
your current working directory. Therefore, setting :envvar:`MPLBACKEND`
globally, e.g. in your :file:`.bashrc` or :file:`.profile`, is discouraged
as it might lead to counter-intuitive behavior.
#. If your script depends on a specific backend you can use the function
:func:`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 `~matplotlib.use` will require changes in your code if users want to
use a different backend. Therefore, you should avoid explicitly calling
`~matplotlib.use` unless absolutely necessary.
.. _the-builtin-backends:
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 :mod:`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
application server
(:doc:`/gallery/user_interfaces/web_application_server_sgskip`), 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
<https://en.wikipedia.org/wiki/Vector_graphics>`_ or `raster
<https://en.wikipedia.org/wiki/Raster_graphics>`_ 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.
Static backends
^^^^^^^^^^^^^^^
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.
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 the pgf_ package.
Cairo png, ps, raster_ or vector_ graphics -- using the Cairo_ library
pdf, svg (requires pycairo_ or cairocffi_).
======== ========= =======================================================
To save plots using the non-interactive backends, use the
``matplotlib.pyplot.savefig('filename')`` method.
Interactive backends
^^^^^^^^^^^^^^^^^^^^
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
``%matplotlib qt``. The Qt binding can be selected via the
:envvar:`QT_API` environment variable; see :ref:`QT_bindings` for
more details.
ipympl Agg rendering embedded in a Jupyter widget (requires ipympl_).
This backend can be enabled in a Jupyter notebook with
``%matplotlib ipympl`` or ``%matplotlib widget``. Works with
Jupyter ``lab`` and ``notebook>=7``.
GTK3Agg Agg rendering to a GTK_ 3.x canvas (requires PyGObject_ and
pycairo_). This backend can be activated in IPython with
``%matplotlib gtk3``.
GTK4Agg Agg rendering to a GTK_ 4.x canvas (requires PyGObject_ and
pycairo_). This backend can be activated in IPython with
``%matplotlib gtk4``.
macosx Agg rendering into a Cocoa canvas in macOS. This backend can be
activated in IPython with ``%matplotlib osx``.
TkAgg Agg rendering to a Tk_ canvas (requires TkInter_). This
backend can be activated in IPython with ``%matplotlib tk``.
nbAgg Embed an interactive figure in a Jupyter classic notebook. This
backend can be enabled in Jupyter notebooks via
``%matplotlib notebook`` or ``%matplotlib nbagg``. Works with
Jupyter ``notebook<7`` and ``nbclassic``.
WebAgg On ``show()`` will start a tornado server with an interactive
figure.
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 ``%matplotlib wx``.
========= ================================================================
.. note::
The names of builtin backends are case-insensitive; e.g., 'QtAgg' and
'qtagg' are equivalent.
.. _`Anti-Grain Geometry`: http://agg.sourceforge.net/antigrain.com/
.. _`Portable Document Format`: https://en.wikipedia.org/wiki/Portable_Document_Format
.. _Postscript: https://en.wikipedia.org/wiki/PostScript
.. _`Scalable Vector Graphics`: https://en.wikipedia.org/wiki/Scalable_Vector_Graphics
.. _pgf: https://ctan.org/pkg/pgf
.. _Cairo: https://www.cairographics.org
.. _PyGObject: https://pygobject.gnome.org/
.. _pycairo: https://www.cairographics.org/pycairo/
.. _cairocffi: https://doc.courtbouillon.org/cairocffi/stable/
.. _wxPython: https://www.wxpython.org/
.. _TkInter: https://docs.python.org/3/library/tk.html
.. _PyQt: https://riverbankcomputing.com/software/pyqt/intro
.. _`Qt for Python`: https://doc.qt.io/qtforpython/
.. _Qt: https://qt.io/
.. _GTK: https://www.gtk.org/
.. _Tk: https://www.tcl.tk/
.. _wxWidgets: https://www.wxwidgets.org/
.. _ipympl: https://www.matplotlib.org/ipympl
.. _ipympl_install:
ipympl
^^^^^^
The ipympl backend is in a separate package that must be explicitly installed
if you wish to use it, for example:
.. code-block:: bash
pip install ipympl
or
.. code-block:: bash
conda install ipympl -c conda-forge
See `installing ipympl <https://matplotlib.org/ipympl/installing.html>`__ 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')``.
Information for backend implementers is available at :ref:`writing_backend_interface`.
.. _figures-not-showing:
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 :ref:`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 :ref:`standalone-scripts-and-interactive-use`), make sure you know which
one is being used:
.. code-block:: python3
import matplotlib
print(matplotlib.get_backend())
Try a simple plot to see if the GUI opens:
.. code-block:: python3
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Test ``PyQt5``.
If you have ``PySide`` or ``PyQt6`` installed rather than ``PyQt5``, just change the import
accordingly:
.. code-block:: bash
python -c "from PyQt5.QtWidgets import *; app = QApplication([]); win = QMainWindow(); win.show(); app.exec()"
TkAgg and TkCairo
^^^^^^^^^^^^^^^^^
Test ``tkinter``:
.. code-block:: bash
python3 -c "from tkinter import Tk; Tk().mainloop()"
GTK3Agg, GTK4Agg, GTK3Cairo, GTK4Cairo
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Test ``Gtk``:
.. code-block:: bash
python3 -c "from gi.repository import Gtk; win = Gtk.Window(); win.connect('destroy', Gtk.main_quit); win.show(); Gtk.main()"
wxAgg and wxCairo
^^^^^^^^^^^^^^^^^
Test ``wx``:
.. code-block:: python3
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
:ref:`get-help`).
|