Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd3ec69b

Browse files
authored
Merge pull request#21335 from jklymak/doc-move-backend
DOC: move usage tutorial info to Users guide rst
2 parents12ff724 +6f4bb05 commitd3ec69b

File tree

5 files changed

+407
-542
lines changed

5 files changed

+407
-542
lines changed

‎doc/users/backends.rst

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
.. _backends:
2+
3+
========
4+
Backends
5+
========
6+
7+
.. _what-is-a-backend:
8+
9+
What is a backend?
10+
------------------
11+
12+
A lot of documentation on the website and in the mailing lists refers
13+
to the "backend" and many new users are confused by this term.
14+
Matplotlib targets many different use cases and output formats. Some
15+
people use Matplotlib interactively from the Python shell and have
16+
plotting windows pop up when they type commands. Some people run
17+
`Jupyter<https://jupyter.org>`_ notebooks and draw inline plots for
18+
quick data analysis. Others embed Matplotlib into graphical user
19+
interfaces like PyQt or PyGObject to build rich applications. Some
20+
people use Matplotlib in batch scripts to generate postscript images
21+
from numerical simulations, and still others run web application
22+
servers to dynamically serve up graphs.
23+
24+
To support all of these use cases, Matplotlib can target different
25+
outputs, and each of these capabilities is called a backend; the
26+
"frontend" is the user facing code, i.e., the plotting code, whereas the
27+
"backend" does all the hard work behind-the-scenes to make the figure.
28+
There are two types of backends: user interface backends (for use in
29+
PyQt/PySide, PyGObject, Tkinter, wxPython, or macOS/Cocoa); also referred to
30+
as "interactive backends") and hardcopy backends to make image files
31+
(PNG, SVG, PDF, PS; also referred to as "non-interactive backends").
32+
33+
Selecting a backend
34+
-------------------
35+
36+
There are three ways to configure your backend:
37+
38+
- The:rc:`backend` parameter in your:file:`matplotlibrc` file
39+
- The:envvar:`MPLBACKEND` environment variable
40+
- The function:func:`matplotlib.use`
41+
42+
Below is a more detailed description.
43+
44+
If there is more than one configuration present, the last one from the
45+
list takes precedence; e.g. calling:func:`matplotlib.use()` will override
46+
the setting in your:file:`matplotlibrc`.
47+
48+
Without a backend explicitly set, Matplotlib automatically detects a usable
49+
backend based on what is available on your system and on whether a GUI event
50+
loop is already running. The first usable backend in the following list is
51+
selected: MacOSX, QtAgg, GTK4Agg, Gtk3Agg, TkAgg, WxAgg, Agg. The last, Agg,
52+
is a non-interactive backend that can only write to files. It is used on
53+
Linux, if Matplotlib cannot connect to either an X display or a Wayland
54+
display.
55+
56+
Here is a detailed description of the configuration methods:
57+
58+
#. Setting:rc:`backend` in your:file:`matplotlibrc` file::
59+
60+
backend : qtagg # use pyqt with antigrain (agg) rendering
61+
62+
See also :doc:`/tutorials/introductory/customizing`.
63+
64+
#. Setting the:envvar:`MPLBACKEND` environment variable:
65+
66+
You can set the environment variable either for your current shell or for
67+
a single script.
68+
69+
On Unix::
70+
71+
> export MPLBACKEND=qtagg
72+
> python simple_plot.py
73+
74+
> MPLBACKEND=qtagg python simple_plot.py
75+
76+
On Windows, only the former is possible::
77+
78+
> set MPLBACKEND=qtagg
79+
> python simple_plot.py
80+
81+
Setting this environment variable will override the ``backend`` parameter
82+
in *any*:file:`matplotlibrc`, even if there is a:file:`matplotlibrc` in
83+
your current working directory. Therefore, setting:envvar:`MPLBACKEND`
84+
globally, e.g. in your:file:`.bashrc` or:file:`.profile`, is discouraged
85+
as it might lead to counter-intuitive behavior.
86+
87+
#. If your script depends on a specific backend you can use the function
88+
:func:`matplotlib.use`::
89+
90+
import matplotlib
91+
matplotlib.use('qtagg')
92+
93+
This should be done before any figure is created, otherwise Matplotlib may
94+
fail to switch the backend and raise an ImportError.
95+
96+
Using `~matplotlib.use` will require changes in your code if users want to
97+
use a different backend. Therefore, you should avoid explicitly calling
98+
`~matplotlib.use` unless absolutely necessary.
99+
100+
.. _the-builtin-backends:
101+
102+
The builtin backends
103+
--------------------
104+
105+
By default, Matplotlib should automatically select a default backend which
106+
allows both interactive work and plotting from scripts, with output to the
107+
screen and/or to a file, so at least initially, you will not need to worry
108+
about the backend. The most common exception is if your Python distribution
109+
comes without:mod:`tkinter` and you have no other GUI toolkit installed.
110+
This happens on certain Linux distributions, where you need to install a
111+
Linux package named ``python-tk`` (or similar).
112+
113+
If, however, you want to write graphical user interfaces, or a web
114+
application server
115+
(:doc:`/gallery/user_interfaces/web_application_server_sgskip`), or need a
116+
better understanding of what is going on, read on. To make things easily
117+
more customizable for graphical user interfaces, Matplotlib separates
118+
the concept of the renderer (the thing that actually does the drawing)
119+
from the canvas (the place where the drawing goes). The canonical
120+
renderer for user interfaces is ``Agg`` which uses the `Anti-Grain
121+
Geometry`_ C++ library to make a raster (pixel) image of the figure; it
122+
is used by the ``QtAgg``, ``GTK4Agg``, ``GTK3Agg``, ``wxAgg``, ``TkAgg``, and
123+
``macosx`` backends. An alternative renderer is based on the Cairo library,
124+
used by ``QtCairo``, etc.
125+
126+
For the rendering engines, users can also distinguish between `vector
127+
<https://en.wikipedia.org/wiki/Vector_graphics>`_ or `raster
128+
<https://en.wikipedia.org/wiki/Raster_graphics>`_ renderers. Vector
129+
graphics languages issue drawing commands like "draw a line from this
130+
point to this point" and hence are scale free. Raster backends
131+
generate a pixel representation of the line whose accuracy depends on a
132+
DPI setting.
133+
134+
Here is a summary of the Matplotlib renderers (there is an eponymous
135+
backend for each; these are *non-interactive backends*, capable of
136+
writing to a file):
137+
138+
======== ========= =======================================================
139+
Renderer Filetypes Description
140+
======== ========= =======================================================
141+
AGG pngraster_ graphics -- high quality images using the
142+
`Anti-Grain Geometry`_ engine
143+
PDF pdfvector_ graphics -- `Portable Document Format`_
144+
PS ps, epsvector_ graphics --Postscript_ output
145+
SVG svgvector_ graphics -- `Scalable Vector Graphics`_
146+
PGF pgf, pdfvector_ graphics -- using thepgf_ package
147+
Cairo png, ps,raster_ orvector_ graphics -- using theCairo_ library
148+
pdf, svg
149+
======== ========= =======================================================
150+
151+
To save plots using the non-interactive backends, use the
152+
``matplotlib.pyplot.savefig('filename')`` method.
153+
154+
These are the user interfaces and renderer combinations supported;
155+
these are *interactive backends*, capable of displaying to the screen
156+
and using appropriate renderers from the table above to write to
157+
a file:
158+
159+
========= ================================================================
160+
Backend Description
161+
========= ================================================================
162+
QtAgg Agg rendering in aQt_ canvas (requiresPyQt_ or `Qt for Python`_,
163+
a.k.a. PySide). This backend can be activated in IPython with
164+
``%matplotlib qt``.
165+
ipympl Agg rendering embedded in a Jupyter widget. (requires ipympl).
166+
This backend can be enabled in a Jupyter notebook with
167+
``%matplotlib ipympl``.
168+
GTK3Agg Agg rendering to aGTK_ 3.x canvas (requiresPyGObject_,
169+
andpycairo_ orcairocffi_). This backend can be activated in
170+
IPython with ``%matplotlib gtk3``.
171+
GTK4Agg Agg rendering to aGTK_ 4.x canvas (requiresPyGObject_,
172+
andpycairo_ orcairocffi_). This backend can be activated in
173+
IPython with ``%matplotlib gtk4``.
174+
macosx Agg rendering into a Cocoa canvas in OSX. This backend can be
175+
activated in IPython with ``%matplotlib osx``.
176+
TkAgg Agg rendering to aTk_ canvas (requiresTkInter_). This
177+
backend can be activated in IPython with ``%matplotlib tk``.
178+
nbAgg Embed an interactive figure in a Jupyter classic notebook. This
179+
backend can be enabled in Jupyter notebooks via
180+
``%matplotlib notebook``.
181+
WebAgg On ``show()`` will start a tornado server with an interactive
182+
figure.
183+
GTK3Cairo Cairo rendering to aGTK_ 3.x canvas (requiresPyGObject_,
184+
andpycairo_ orcairocffi_).
185+
GTK4Cairo Cairo rendering to aGTK_ 4.x canvas (requiresPyGObject_,
186+
andpycairo_ orcairocffi_).
187+
wxAgg Agg rendering to awxWidgets_ canvas (requireswxPython_ 4).
188+
This backend can be activated in IPython with ``%matplotlib wx``.
189+
========= ================================================================
190+
191+
..note::
192+
The names of builtin backends case-insensitive; e.g., 'QtAgg' and
193+
'qtagg' are equivalent.
194+
195+
.. _`Anti-Grain Geometry`:http://agg.sourceforge.net/antigrain.com/
196+
.. _`Portable Document Format`:https://en.wikipedia.org/wiki/Portable_Document_Format
197+
.. _Postscript:https://en.wikipedia.org/wiki/PostScript
198+
.. _`Scalable Vector Graphics`:https://en.wikipedia.org/wiki/Scalable_Vector_Graphics
199+
.. _pgf:https://ctan.org/pkg/pgf
200+
.. _Cairo:https://www.cairographics.org
201+
.. _PyGObject:https://wiki.gnome.org/action/show/Projects/PyGObject
202+
.. _pycairo:https://www.cairographics.org/pycairo/
203+
.. _cairocffi:https://pythonhosted.org/cairocffi/
204+
.. _wxPython:https://www.wxpython.org/
205+
.. _TkInter:https://docs.python.org/3/library/tk.html
206+
.. _PyQt:https://riverbankcomputing.com/software/pyqt/intro
207+
.. _`Qt for Python`:https://doc.qt.io/qtforpython/
208+
.. _Qt:https://qt.io/
209+
.. _GTK:https://www.gtk.org/
210+
.. _Tk:https://www.tcl.tk/
211+
.. _wxWidgets:https://www.wxwidgets.org/
212+
213+
ipympl
214+
^^^^^^
215+
216+
The Jupyter widget ecosystem is moving too fast to support directly in
217+
Matplotlib. To install ipympl:
218+
219+
..code-block::bash
220+
221+
pip install ipympl
222+
jupyter nbextensionenable --py --sys-prefix ipympl
223+
224+
or
225+
226+
..code-block::bash
227+
228+
conda install ipympl -c conda-forge
229+
230+
See `jupyter-matplotlib<https://github.com/matplotlib/jupyter-matplotlib>`__
231+
for more details.
232+
233+
.. _QT_API-usage:
234+
235+
How do I select PyQt5 or PySide2?
236+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
237+
238+
The:envvar:`QT_API` environment variable can be set to either ``pyqt5`` or
239+
``pyside2`` to use ``PyQt5`` or ``PySide2``, respectively.
240+
241+
Since the default value for the bindings to be used is ``PyQt5``, Matplotlib
242+
first tries to import it. If the import fails, it tries to import
243+
``PySide2``.
244+
245+
Using non-builtin backends
246+
--------------------------
247+
More generally, any importable backend can be selected by using any of the
248+
methods above. If ``name.of.the.backend`` is the module containing the
249+
backend, use ``module://name.of.the.backend`` as the backend name, e.g.
250+
``matplotlib.use('module://name.of.the.backend')``.

‎doc/users/getting_started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ Installation
2222
2323
conda install matplotlib
2424
25-
Further details are available in the:doc:`Installation Guide</users/installing>`.
26-
25+
Further details are available in the:doc:`Installation Guide</users/installing>`.
2726

2827
Draw a first plot
2928
-----------------
@@ -44,6 +43,7 @@ Here is a minimal example plot you can try out:
4443
ax.plot(x, y)
4544
plt.show()
4645

46+
If a plot does not show up please check:ref:`troubleshooting-faq`.
4747

4848
Where to go next
4949
----------------

‎doc/users/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ Usage guide
1111
../tutorials/index.rst
1212
../gallery/index.rst
1313
getting_started.rst
14+
backends.rst
15+
performance.rst
1416
explain.rst
1517
../faq/index.rst
1618
../api/index.rst
1719
../resources/index.rst
20+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp