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 embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, and still others in 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 pygtk, wxpython, tkinter, qt4, or macosx; also referred to as “interactive backends”) and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”).
There are four ways to configure your backend. If they conflict each other, the method mentioned last in the following list will be used, e.g. calling use() will override the setting in your matplotlibrc.
The backend parameter in your matplotlibrc file (see Customizing matplotlib):1backend : WXAgg # use wxpython with antigrain (agg) rendering
Setting the MPLBACKEND environment variable, either for your current shell or for a single script:123> export MPLBACKEND="module://my_backend"> python simple_plot.py> MPLBACKEND="module://my_backend" python simple_plot.py
Setting this environment variable will override the backend parameter in any matplotlibrc, even if there is a matplotlibrc in your current working directory. Therefore setting MPLBACKEND globally, e.g. in your .bashrc or .profile, is discouraged as it might lead to counter-intuitive behavior.
To set the backend for a single script, you can alternatively use the -d command line argument:1> python script.py -dbackend
This method is deprecated as the -d argument might conflict with scripts which parse command line arguments (see issue #1986). You should use MPLBACKEND instead.
- If your script depends on a specific backend you can use the use() function:12import matplotlibmatplotlib.use('PS') # generate postscript output by default
If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect. Using use() will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling use() unless absolutely necessary.
Backend name specifications are not case-sensitive; e.g., ‘GTKAgg’ and ‘gtkagg’ are equivalent.
With a typical installation of matplotlib, such as from a binary installer or a linux distribution package, a good default backend will already be set, allowing 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 use any of the methods given above.
If, however, you want to write graphical user interfaces, or a web application server (Matplotlib in a web application server), or need a better understanding of what is going on, read on. To make things a little 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. All of the user interfaces except macosx can be used with agg rendering, e.g., WXAgg, GTKAgg, QT4Agg, QT5Agg, TkAgg. In addition, some of the user interfaces support other rendering engines. For example, with GTK, you can also select GDK rendering (backend GTK deprecated in 2.0) or Cairo rendering (backend GTKCairo).
For the rendering engines, one 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, and 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 backed for each; these are non-interactive backends, capable of writing to a file):
|AGG||png||raster graphics – high quality images using the Anti-Grain Geometry engine|
|PS||ps eps||vector graphics – Postscript output|
|vector graphics – Portable Document Format|
|SVG||svg||vector graphics – Scalable Vector Graphics|
|Cairo||png ps pdf svg||vector graphics – Cairo graphics|
|GDK||png jpg tiff||raster graphics – the Gimp Drawing Kit Deprecated in 2.0|
And here are the user interfaces and renderer combinations supported; these are interactive backends, capable of displaying to the screen and of using appropriate renderers from the table above to write to a file:
|GTKAgg||Agg rendering to a GTK 2.x canvas (requires PyGTK and pycairo or cairocffi; Python2 only)|
|GTK3Agg||Agg rendering to a GTK 3.x canvas (requires PyGObject and pycairo or cairocffi)|
|GTK||GDK rendering to a GTK 2.x canvas (not recommended and d eprecated in 2.0) (requires PyGTK and pycairo or cairocffi; Python2 only)|
|GTKCairo||Cairo rendering to a GTK 2.x canvas (requires PyGTK and pycairo or cairocffi; Python2 only)|
|GTK3Cairo||Cairo rendering to a GTK 3.x canvas (requires PyGObject and pycairo or cairocffi)|
|WXAgg||Agg rendering to to a wxWidgets canvas (requires wxPython)|
|WX||Native wxWidgets drawing to a wxWidgets Canvas (not recommended and deprecated in 2.0) (requires wxPython)|
|TkAgg||Agg rendering to a Tk canvas (requires TkInter)|
|Qt4Agg||Agg rendering to a Qt4 canvas (requires PyQt4 or pyside)|
|Qt5Agg||Agg rendering in a Qt5 canvas (requires PyQt5)|
|macosx||Cocoa rendering in OSX windows (presently lacks blocking show() behavior when matplotlib is in non-interactive mode)|
Matplotlib is an open source library for easy plotting. We can integrate Matplotlib into Pygame game and create various plots.
In order to integrate Matplotlib with Pygame, we need to use a non-interactive backend, otherwise Matplotlib will present us with a GUI window by default. We will import the main Matplotlib module and call the
use function. This function has to be called immediately after importing the main
matplotlib module and before other
matplotlib modules are imported:
notice: non-interactive backend, meaning it won’t display on the screen, only save to files. If we do not change the default backend. Python will give us