9
9
.. toctree ::
10
10
11
11
12
- When working with data, interactivity can be invaluable. The pan/zoom and
13
- mouse-location tools built into the Matplotlib GUI windows are often sufficient, but
12
+ When working with data, interactivity can be invaluable. The pan/zoom and
13
+ mouse-location tools built into the Matplotlib GUI windows are often sufficient, but
14
14
you can also use the event system to build customized data exploration tools.
15
15
16
16
Matplotlib ships with :ref: `backends <what-is-a-backend >` binding to
17
17
several GUI toolkits (Qt, Tk, Wx, GTK, macOS, JavaScript) and third party
18
18
packages provide bindings to `kivy
19
19
<https://github.com/kivy-garden/garden.matplotlib> `__ and `Jupyter Lab
20
- <https://github.com/matplotlib/ipympl> `__. For the figures to be interactive,
21
- the GUI event loop will need to be integrated with your interactive prompt.
22
- We recommend using IPython (see :ref: `below <ipython-pylab >`).
20
+ <https://github.com/matplotlib/ipympl> `__. For the figures to be responsive to
21
+ mouse, keyboard, and paint events, the GUI event loop needs to be integrated
22
+ with an interactive prompt. We recommend using IPython (see :ref: `below <ipython-pylab >`).
23
23
24
- The `.pyplot ` module provides functions for explicitly creating figures
24
+ The `.pyplot ` module provides functions for explicitly creating figures
25
25
that include interactive tools, a toolbar, a tool-tip, and
26
26
:ref: `key bindings <key-event-handling >`:
27
27
28
- * `.pyplot.figure `
28
+ `.pyplot.figure `
29
29
Creates a new empty `.figure.Figure ` or selects an existing figure
30
30
31
- * `.pyplot.subplots `
31
+ `.pyplot.subplots `
32
32
Creates a new `.figure.Figure ` and fills it with a grid of `.axes.Axes `
33
33
34
34
`.pyplot ` has a notion of "The Current Figure" which can be accessed
35
35
through `.pyplot.gcf ` and a notion of "The Current Axes" accessed
36
36
through `.pyplot.gca `. Almost all of the functions in `.pyplot ` pass
37
37
through the current `.Figure ` / `.axes.Axes ` (or create one) as
38
- appropriate.
38
+ appropriate.
39
39
40
40
Matplotlib keeps a reference to all of the open figures
41
- created this way so they will not be garbage collected. You can close
42
- and deregister `.Figure `\s from `.pyplot ` individually via
43
- `.pyplot.close ` or close all open figures via ``plt.close('all') ``.
41
+ created via ` pyplot.figure ` or ` pyplot.subplots ` so that the figures will not be garbage
42
+ collected. `.Figure `\s can be closed and deregistered from `.pyplot ` individually via
43
+ `.pyplot.close `; all open ` .Figure ` \s can be closed via ``plt.close('all') ``.
44
44
45
45
46
46
For more discussion of Matplotlib's event system and integrated event loops, please read:
@@ -58,12 +58,14 @@ IPython integration
58
58
===================
59
59
60
60
We recommend using IPython for an interactive shell. In addition to
61
- all of its features (improved tab-completion, magics,
62
- multiline editing, etc), it also ensures that the GUI toolkit event
63
- loop is properly integrated with the command line (see
64
- :ref: `cp_integration `). To configure the integration and enable
65
- :ref: `interactive mode <controlling-interactive >` use the
66
- ``%matplotlib `` magic
61
+ all of its features (improved tab-completion, magics, multiline editing, etc),
62
+ it also ensures that the GUI toolkit event loop is properly integrated
63
+ with the command line (see :ref: `cp_integration `).
64
+
65
+ In this example, we create and modify a figure via an IPython prompt.
66
+ The figure displays in a Qt5Agg GUI window. To configure the integration
67
+ and enable :ref: `interactive mode <controlling-interactive >` use the
68
+ ``%matplotlib `` magic:
67
69
68
70
.. highlight :: ipython
69
71
@@ -92,22 +94,22 @@ Change the color of the line from blue to orange:
92
94
93
95
In [5]: ln.set_color('orange')
94
96
95
- If you wish to disable interactive modification of the plot:
97
+ If you wish to disable automatic redrawing of the plot:
96
98
97
99
::
98
100
99
101
In [6]: plt.ioff()
100
102
101
- If you wish to re-enable interactive "live" modification of the plot:
103
+ If you wish to re-enable automatic redrawing of the plot:
102
104
103
105
::
104
106
105
107
In [7]: plt.ion()
106
108
107
109
108
110
In recent versions of ``Matplotlib `` and ``IPython ``, it is
109
- sufficient to import `matplotlib.pyplot ` and call `.pyplot.ion `.
110
- Using the ``% `` magic is guaranteed to work in all versions of `` Matplotlib `` and `` IPython `` .
111
+ sufficient to import `matplotlib.pyplot ` and call `.pyplot.ion `.
112
+ Using the ``% `` magic is guaranteed to work in all versions of Matplotlib and IPython.
111
113
112
114
113
115
.. highlight :: python
@@ -139,43 +141,43 @@ Interactive mode controls:
139
141
140
142
- whether created figures are automatically shown
141
143
- whether changes to artists automatically trigger re-drawing existing figures
142
- - when `.pyplot.show ` exits : immediately, or after all of the figures have been closed if given no arguments
144
+ - when `.pyplot.show() ` returns if given no arguments : immediately, or after all of the figures have been closed
143
145
144
- Interactive mode:
146
+ If in interactive mode:
145
147
146
148
- newly created figures will be displayed immediately
147
149
- figures will automatically redraw when elements are changed
148
- - `pyplot.show ` displays the figures and immediately returns to the prompt
149
-
150
- Not in interactive mode:
150
+ - `pyplot.show() ` displays the figures and immediately returns
151
151
152
- - newly created figures and changes to figures are not displayed until
153
- `.pyplot.show ` is called again or `.pyplot.pause ` exits
154
- - `pyplot.show ` runs the GUI event loop and does not return until all
155
- the plot windows are closed
152
+ If not in interactive mode:
156
153
154
+ - newly created figures and changes to figures are not displayed until
155
+ * `.pyplot.show() ` is called
156
+ * `.pyplot.pause() ` is called
157
+ * `.FigureCanvasBase.flush_events() ` is called
158
+ - `pyplot.show() ` runs the GUI event loop and does not return until all the plot windows are closed
157
159
158
160
If you are in non-interactive mode (or created figures while in
159
161
non-interactive mode) you may need to explicitly call `.pyplot.show `
160
162
to display the windows on your screen. If you only want to run the
161
163
GUI event loop for a fixed amount of time, you can use `.pyplot.pause `.
162
164
This will block the progress of your code as if you had called
163
- `time.sleep `, ensure the current window is shown and re-drawn if needed,
164
- and run the GUI event loop (so the windows are "live" for
165
- interactive modification) for the specified period of time.
166
-
167
- The GUI event loop being integrated with your command prompt and
168
- the figures being in interactive mode are independent of each other.
169
- If you use `pyplot.ion ` but have not arranged for the event loop integration,
170
- your figures will appear but will not be "live" while the prompt is waiting for input.
165
+ `time.sleep `, ensure the current window is shown and re-drawn if needed,
166
+ and run the GUI event loop for the specified period of time.
167
+
168
+ The GUI event loop being integrated with your command prompt and
169
+ the figures being in interactive mode are independent of each other.
170
+ If you use `pyplot.ion ` but have not arranged for the event loop integration,
171
+ your figures will appear but will not be interactive while the prompt is waiting for input.
171
172
You will not be able to pan/zoom and the figure may not even render
172
173
(the window might appear black, transparent, or as a snapshot of the
173
174
desktop under it). Conversely, if you configure the event loop
174
- integration, displayed figures will be "live" while waiting for input
175
- at the prompt, regardless of pyplot's "interactive mode".
175
+ integration, displayed figures will be responsive while waiting for input
176
+ at the prompt, regardless of pyplot's "interactive mode".
176
177
177
- The figures will also be "live" if you use ``pyplot.show(block=True) ``,
178
- `.pyplot.pause `, or run the the GUI main loop in some other way.
178
+ No matter what combination of interactive mode setting and event loop integration,
179
+ figures will be responsive if you use ``pyplot.show(block=True) ``, `.pyplot.pause `, or run
180
+ the GUI main loop in some other way.
179
181
180
182
181
183
.. warning ::
@@ -255,11 +257,11 @@ Jupyter Notebooks / Lab
255
257
using an interactive backend. The default backend in notebooks,
256
258
the inline backend, is not. `~ipykernel.pylab.backend_inline `
257
259
renders the figure once and inserts a static image into the
258
- notebook when the cell is executed. Because the images are static, they
260
+ notebook when the cell is executed. Because the images are static, they
259
261
can not be panned / zoomed, take user input, or be updated from other
260
262
cells.
261
263
262
- To get interactive figures in the 'classic' notebook or Jupyter lab,
264
+ To get interactive figures in the 'classic' notebook or Jupyter lab,
263
265
use the `ipympl <https://github.com/matplotlib/ipympl >`__ backend
264
266
(must be installed separately) which uses the **ipywidget ** framework.
265
267
If ``ipympl `` is installed use the magic:
@@ -276,17 +278,17 @@ If you only need to use the classic notebook, you can use
276
278
277
279
%matplotlib notebook
278
280
279
- which uses the `.backend_nbagg ` backend which is installed by Matplotlib;
281
+ which uses the `.backend_nbagg ` backend provided by Matplotlib;
280
282
however, nbagg does not work in Jupyter Lab.
281
283
282
284
GUIs + Jupyter
283
285
~~~~~~~~~~~~~~
284
286
285
- You can also use one of the non-ipympl GUI backends in a Jupyter Notebook.
286
- If you are running your Jupyter kernel locally, the GUI window will spawn on
287
- your desktop adjacent to your web browser. If you run your notebook on a remote server,
288
- the kernel will try to open the GUI window on the remote computer. Unless you have
289
- arranged to forward the xserver back to your desktop, you will not be able to
287
+ You can also use one of the non-`` ipympl `` GUI backends in a Jupyter Notebook.
288
+ If you are running your Jupyter kernel locally, the GUI window will spawn on
289
+ your desktop adjacent to your web browser. If you run your notebook on a remote server,
290
+ the kernel will try to open the GUI window on the remote computer. Unless you have
291
+ arranged to forward the xserver back to your desktop, you will not be able to
290
292
see or interact with the window. It may also raise an exception.
291
293
292
294
0 commit comments