Skip to content

Commit f8e4012

Browse files
story645tacaswelldstansbyQuLogic
authored
Removed idiomatic constructs from interactive figures docs (#17471)
* cleaned up idioms, restructured the code example, stripped down some langauge Co-authored-by: Thomas A Caswell <tcaswell@gmail.com> Co-authored-by: David Stansby <dstansby@gmail.com> Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 697b34b commit f8e4012

File tree

2 files changed

+88
-87
lines changed

2 files changed

+88
-87
lines changed

doc/devel/documenting_mpl.rst

+12-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ it, use
111111
.. code-block:: sh
112112
113113
make SPHINXOPTS= html
114+
115+
On Windows the arguments must be at the end of the statement:
116+
117+
.. code-block:: bat
118+
119+
make html SPHINXOPTS=
114120
115121
You can use the ``O`` variable to set additional options:
116122

@@ -121,8 +127,12 @@ You can use the ``O`` variable to set additional options:
121127
Multiple options can be combined using e.g. ``make O='-j4 -Dplot_gallery=0'
122128
html``.
123129

124-
On Windows, options needs to be set as environment variables, e.g. ``set O=-W
125-
--keep-going -j4 & make html``.
130+
On Windows, either use the format shown above or set options as environment variables, e.g.:
131+
132+
.. code-block:: bat
133+
134+
set O=-W --keep-going -j4
135+
make html
126136
127137
.. _writing-rest-pages:
128138

doc/users/interactive.rst

+76-85
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@
99
.. toctree::
1010

1111

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.
1615

1716
Matplotlib ships with :ref:`backends <what-is-a-backend>` binding to
1817
several GUI toolkits (Qt, Tk, Wx, GTK, macOS, JavaScript) and third party
1918
packages provide bindings to `kivy
2019
<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>`).
2423

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>`:
2827

2928
`.pyplot.figure`
3029
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
3635
through `.pyplot.gcf` and a notion of "The Current Axes" accessed
3736
through `.pyplot.gca`. Almost all of the functions in `.pyplot` pass
3837
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.
4339

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:
4647

4748
.. toctree::
4849
:maxdepth: 1
@@ -57,64 +58,58 @@ IPython integration
5758
===================
5859

5960
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:
6669

6770
.. highlight:: ipython
6871

6972
::
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-
7673
In [1]: %matplotlib
7774
Using matplotlib backend: Qt5Agg
7875

7976
In [2]: import matplotlib.pyplot as plt
8077

81-
Calling
78+
Create a new figure window:
8279

8380
::
8481

8582
In [3]: fig, ax = plt.subplots()
8683

87-
will pop open a window for you and
84+
85+
Add a line plot of the data to the window:
8886

8987
::
9088

9189
In [4]: ln, = ax.plot(range(5))
9290

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:
9592

9693
::
9794

9895
In [5]: ln.set_color('orange')
9996

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:
10298

10399
::
104100

105101
In [6]: plt.ioff()
106102

107-
and
103+
If you wish to re-enable automatic redrawing of the plot:
108104

109105
::
110106

111107
In [7]: plt.ion()
112108

113-
re-enable it.
114109

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.
118113

119114

120115
.. highlight:: python
@@ -146,45 +141,43 @@ Interactive mode controls:
146141

147142
- whether created figures are automatically shown
148143
- 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
152145

153-
If in interactive mode, then:
146+
If in interactive mode:
154147

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
158151

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:
165153

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
166159

167160
If you are in non-interactive mode (or created figures while in
168161
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.
180172
You will not be able to pan/zoom and the figure may not even render
181173
(the window might appear black, transparent, or as a snapshot of the
182174
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.
188181

189182

190183
.. warning::
@@ -201,7 +194,7 @@ Default UI
201194

202195

203196
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
205198
helpful keybindings are registered by default.
206199

207200

@@ -240,8 +233,7 @@ Preserve aspect ratio hold **CONTROL** when panning/zooming with mo
240233
Other Python prompts
241234
====================
242235

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:
245237

246238

247239
.. sourcecode:: pycon
@@ -265,11 +257,11 @@ Jupyter Notebooks / Lab
265257
using an interactive backend. The default backend in notebooks,
266258
the inline backend, is not. `~ipykernel.pylab.backend_inline`
267259
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
270262
cells.
271263

272-
To get interactive figures in the 'classic' notebook or jupyter lab
264+
To get interactive figures in the 'classic' notebook or Jupyter lab,
273265
use the `ipympl <https://github.com/matplotlib/ipympl>`__ backend
274266
(must be installed separately) which uses the **ipywidget** framework.
275267
If ``ipympl`` is installed use the magic:
@@ -280,25 +272,24 @@ If ``ipympl`` is installed use the magic:
280272

281273
to select and enable it.
282274

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
284276

285277
.. sourcecode:: ipython
286278

287279
%matplotlib notebook
288280

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.
291283

292-
GUIs + jupyter
284+
GUIs + Jupyter
293285
~~~~~~~~~~~~~~
294286

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.
302293

303294

304295

0 commit comments

Comments
 (0)