9
9
.. toctree ::
10
10
11
11
12
- When working with data it is often invaluable to be able to interact
13
- with your plots. In many cases the built in pan/zoom and mouse-location
14
- tools are sufficient, but you can also use the Matplotlib event system
15
- to build customized data exploration tools.
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
+ you can also use the event system to build customized data exploration tools.
16
15
17
16
Matplotlib ships with :ref: `backends <what-is-a-backend >` binding to
18
17
several GUI toolkits (Qt, Tk, Wx, GTK, macOS, JavaScript) and third party
19
18
packages provide bindings to `kivy
20
19
<https://github.com/kivy-garden/garden.matplotlib> `__ and `Jupyter Lab
21
- <https://github.com/matplotlib/ipympl> `__. For the figures to be
22
- "live" the GUI event loop will need to be integrated with your prompt. The
23
- simplest way is to use 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 >`).
24
23
25
- The `.pyplot ` module provides functions for explicitly creating
26
- Figures that include interactive tools, a toolbar, a tool-tip, and
27
- :ref: `key bindings <key-event-handling >` ready to go :
24
+ The `.pyplot ` module provides functions for explicitly creating figures
25
+ that include interactive tools, a toolbar, a tool-tip, and
26
+ :ref: `key bindings <key-event-handling >`:
28
27
29
28
`.pyplot.figure `
30
29
Creates a new empty `.figure.Figure ` or selects an existing figure
@@ -36,13 +35,15 @@ Figures that include interactive tools, a toolbar, a tool-tip, and
36
35
through `.pyplot.gcf ` and a notion of "The Current Axes" accessed
37
36
through `.pyplot.gca `. Almost all of the functions in `.pyplot ` pass
38
37
through the current `.Figure ` / `.axes.Axes ` (or create one) as
39
- appropriate. Matplotlib keeps a reference to all of the open figures
40
- created this way so they will not be garbage collected. You can close
41
- and deregister `.Figure `\s from `.pyplot ` individually via
42
- `.pyplot.close ` or close all open figures via ``plt.close('all') ``.
38
+ appropriate.
43
39
44
- For discussion of how the integration of the event loops and Matplotlib's event
45
- system work under the hood see:
40
+ Matplotlib keeps a reference to all of the open figures
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
+
45
+
46
+ For more discussion of Matplotlib's event system and integrated event loops, please read:
46
47
47
48
.. toctree ::
48
49
:maxdepth: 1
@@ -57,64 +58,58 @@ IPython integration
57
58
===================
58
59
59
60
We recommend using IPython for an interactive shell. In addition to
60
- all of its features (improved tab-completion, magics,
61
- multiline editing, etc), it also ensures that the GUI toolkit event
62
- loop is properly integrated with the command line (see
63
- :ref: `cp_integration `). To configure the integration and enable
64
- :ref: `interactive mode <controlling-interactive >` use the
65
- ``%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:
66
69
67
70
.. highlight :: ipython
68
71
69
72
::
70
-
71
- user@machine:~ $ ipython
72
- Python 3.8.2 (default, Apr 8 2020, 14:31:25)
73
- Type 'copyright', 'credits' or 'license' for more information
74
- IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
75
-
76
73
In [1]: %matplotlib
77
74
Using matplotlib backend: Qt5Agg
78
75
79
76
In [2]: import matplotlib.pyplot as plt
80
77
81
- Calling
78
+ Create a new figure window:
82
79
83
80
::
84
81
85
82
In [3]: fig, ax = plt.subplots()
86
83
87
- will pop open a window for you and
84
+
85
+ Add a line plot of the data to the window:
88
86
89
87
::
90
88
91
89
In [4]: ln, = ax.plot(range(5))
92
90
93
- will show your data in the window. If you change something about the
94
- line, for example the color
91
+ Change the color of the line from blue to orange:
95
92
96
93
::
97
94
98
95
In [5]: ln.set_color('orange')
99
96
100
- it will be reflected immediately. If you wish to disable this behavior
101
- use
97
+ If you wish to disable automatic redrawing of the plot:
102
98
103
99
::
104
100
105
101
In [6]: plt.ioff()
106
102
107
- and
103
+ If you wish to re-enable automatic redrawing of the plot:
108
104
109
105
::
110
106
111
107
In [7]: plt.ion()
112
108
113
- re-enable it.
114
109
115
- With recent versions of ``Matplotlib `` and ``IPython `` it is
116
- sufficient to import `matplotlib.pyplot ` and call `.pyplot.ion `, but
117
- using the magic is guaranteed to work regardless of versions.
110
+ In recent versions of ``Matplotlib `` and ``IPython ``, it is
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 .
118
113
119
114
120
115
.. highlight :: python
@@ -146,45 +141,43 @@ Interactive mode controls:
146
141
147
142
- whether created figures are automatically shown
148
143
- whether changes to artists automatically trigger re-drawing existing figures
149
- - whether `.pyplot.show ` returns immediately or after all of the
150
- figures have been closed when given no arguments
151
-
144
+ - when `.pyplot.show() ` returns if given no arguments: immediately, or after all of the figures have been closed
152
145
153
- If in interactive mode, then :
146
+ If in interactive mode:
154
147
155
- - newly created figures will be shown immediately
156
- - figures will automatically redraw on change
157
- - pyplot.show will return immediately by default
148
+ - newly created figures will be displayed immediately
149
+ - figures will automatically redraw when elements are changed
150
+ - ` pyplot.show() ` displays the figures and immediately returns
158
151
159
- If not in interactive mode then:
160
-
161
- - newly created figures and changes to figures will
162
- not be reflected until explicitly asked to be
163
- - pyplot.show runs the GUI event loop and does not return until all of
164
- the plot windows are closed
152
+ If not in interactive mode:
165
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
166
159
167
160
If you are in non-interactive mode (or created figures while in
168
161
non-interactive mode) you may need to explicitly call `.pyplot.show `
169
- to bring the windows onto your screen. If you only want to run the
170
- GUI event loop for a fixed amount of time you can use `.pyplot.pause `.
171
- This will both block the progress of your code (as if you had called
172
- `time.sleep `), ensure the current window is shown and if needed
173
- re-drawn, and run the GUI event loop (so the windows are "live" for
174
- interaction) for the specified period of time.
175
-
176
- Being in "interactive mode" is orthogonal to the GUI event loop being
177
- integrated with your command prompt. If you use `pyplot.ion `, but
178
- have not arranged for the event loop integration, your figures will
179
- appear but will not be "live" while the prompt is waiting for input.
162
+ to display the windows on your screen. If you only want to run the
163
+ GUI event loop for a fixed amount of time, you can use `.pyplot.pause `.
164
+ This will block the progress of your code as if you had called
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.
180
172
You will not be able to pan/zoom and the figure may not even render
181
173
(the window might appear black, transparent, or as a snapshot of the
182
174
desktop under it). Conversely, if you configure the event loop
183
- integration, displayed figures will be "live" while waiting for input
184
- at the prompt, regardless of pyplot's "interactive mode". In either
185
- case, the figures will be "live" if you use
186
- ``pyplot.show(block=True) ``, `.pyplot.pause `, or run the the GUI main
187
- loop in some other way.
175
+ integration, displayed figures will be responsive while waiting for input
176
+ at the prompt, regardless of pyplot's "interactive mode".
177
+
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.
188
181
189
182
190
183
.. warning ::
@@ -201,7 +194,7 @@ Default UI
201
194
202
195
203
196
The windows created by :mod: `~.pyplot ` have an interactive toolbar with navigation
204
- buttons and a readout of where the cursor is in dataspace . A number of
197
+ buttons and a readout of the data values the cursor is pointing at . A number of
205
198
helpful keybindings are registered by default.
206
199
207
200
@@ -240,8 +233,7 @@ Preserve aspect ratio hold **CONTROL** when panning/zooming with mo
240
233
Other Python prompts
241
234
====================
242
235
243
- If you can not or do not want to use IPython, interactive mode works
244
- in the vanilla python prompt
236
+ Interactive mode works in the default Python prompt:
245
237
246
238
247
239
.. sourcecode :: pycon
@@ -265,11 +257,11 @@ Jupyter Notebooks / Lab
265
257
using an interactive backend. The default backend in notebooks,
266
258
the inline backend, is not. `~ipykernel.pylab.backend_inline `
267
259
renders the figure once and inserts a static image into the
268
- notebook when the cell is executed. The images are static and can
269
- not be panned / zoomed, take user input, or be updated from other
260
+ notebook when the cell is executed. Because the images are static, they
261
+ can not be panned / zoomed, take user input, or be updated from other
270
262
cells.
271
263
272
- To get interactive figures in the 'classic' notebook or jupyter lab
264
+ To get interactive figures in the 'classic' notebook or Jupyter lab,
273
265
use the `ipympl <https://github.com/matplotlib/ipympl >`__ backend
274
266
(must be installed separately) which uses the **ipywidget ** framework.
275
267
If ``ipympl `` is installed use the magic:
@@ -280,25 +272,24 @@ If ``ipympl`` is installed use the magic:
280
272
281
273
to select and enable it.
282
274
283
- If you only need to use the classic notebook you can use
275
+ If you only need to use the classic notebook, you can use
284
276
285
277
.. sourcecode :: ipython
286
278
287
279
%matplotlib notebook
288
280
289
- which uses the `.backend_nbagg ` backend which ships with Matplotlib.
290
- However nbagg does not work in Jupyter Lab.
281
+ which uses the `.backend_nbagg ` backend provided by Matplotlib;
282
+ however, nbagg does not work in Jupyter Lab.
291
283
292
- GUIs + jupyter
284
+ GUIs + Jupyter
293
285
~~~~~~~~~~~~~~
294
286
295
- If you are running your jupyter kernel locally you can use one of the
296
- GUI backends. The process running your kernel will show a GUI window
297
- on your desktop adjacent to your web browser. However if you move
298
- that notebook to a remote server the kernel will try to open the GUI
299
- window on *that * computer. Unless you have arranged to forward the
300
- xserver back to your desktop, you not be able to see or interact with
301
- the figure (if it does not raise an exception outright).
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
292
+ see or interact with the window. It may also raise an exception.
302
293
303
294
304
295
0 commit comments