Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6411d4

Browse files
story645tacaswelldstansbyQuLogic
committed
Update doc/users/interactive.rst
Co-authored-by: Thomas A Caswell <[email protected]> Co-authored-by: David Stansby <[email protected]> Co-authored-by: Elliott Sales de Andrade <[email protected]>
1 parent bd5d63a commit e6411d4

File tree

1 file changed

+53
-51
lines changed

1 file changed

+53
-51
lines changed

doc/users/interactive.rst

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@
99
.. toctree::
1010

1111

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
1414
you can also use the event system to build customized data exploration tools.
1515

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

24-
The `.pyplot` module provides functions for explicitly creating figures
24+
The `.pyplot` module provides functions for explicitly creating figures
2525
that include interactive tools, a toolbar, a tool-tip, and
2626
:ref:`key bindings <key-event-handling>`:
2727

28-
* `.pyplot.figure`
28+
`.pyplot.figure`
2929
Creates a new empty `.figure.Figure` or selects an existing figure
3030

31-
* `.pyplot.subplots`
31+
`.pyplot.subplots`
3232
Creates a new `.figure.Figure` and fills it with a grid of `.axes.Axes`
3333

3434
`.pyplot` has a notion of "The Current Figure" which can be accessed
3535
through `.pyplot.gcf` and a notion of "The Current Axes" accessed
3636
through `.pyplot.gca`. Almost all of the functions in `.pyplot` pass
3737
through the current `.Figure` / `.axes.Axes` (or create one) as
38-
appropriate.
38+
appropriate.
3939

4040
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')``.
4444

4545

4646
For more discussion of Matplotlib's event system and integrated event loops, please read:
@@ -58,12 +58,14 @@ IPython integration
5858
===================
5959

6060
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:
6769

6870
.. highlight:: ipython
6971

@@ -92,22 +94,22 @@ Change the color of the line from blue to orange:
9294

9395
In [5]: ln.set_color('orange')
9496

95-
If you wish to disable interactive modification of the plot:
97+
If you wish to disable automatic redrawing of the plot:
9698

9799
::
98100

99101
In [6]: plt.ioff()
100102

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

103105
::
104106

105107
In [7]: plt.ion()
106108

107109

108110
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.
111113

112114

113115
.. highlight:: python
@@ -139,43 +141,43 @@ Interactive mode controls:
139141

140142
- whether created figures are automatically shown
141143
- 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
143145

144-
Interactive mode:
146+
If in interactive mode:
145147

146148
- newly created figures will be displayed immediately
147149
- 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
151151

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

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
157159

158160
If you are in non-interactive mode (or created figures while in
159161
non-interactive mode) you may need to explicitly call `.pyplot.show`
160162
to display the windows on your screen. If you only want to run the
161163
GUI event loop for a fixed amount of time, you can use `.pyplot.pause`.
162164
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.
171172
You will not be able to pan/zoom and the figure may not even render
172173
(the window might appear black, transparent, or as a snapshot of the
173174
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".
176177

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

180182

181183
.. warning::
@@ -255,11 +257,11 @@ Jupyter Notebooks / Lab
255257
using an interactive backend. The default backend in notebooks,
256258
the inline backend, is not. `~ipykernel.pylab.backend_inline`
257259
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
259261
can not be panned / zoomed, take user input, or be updated from other
260262
cells.
261263

262-
To get interactive figures in the 'classic' notebook or Jupyter lab,
264+
To get interactive figures in the 'classic' notebook or Jupyter lab,
263265
use the `ipympl <https://github.com/matplotlib/ipympl>`__ backend
264266
(must be installed separately) which uses the **ipywidget** framework.
265267
If ``ipympl`` is installed use the magic:
@@ -276,17 +278,17 @@ If you only need to use the classic notebook, you can use
276278

277279
%matplotlib notebook
278280

279-
which uses the `.backend_nbagg` backend which is installed by Matplotlib;
281+
which uses the `.backend_nbagg` backend provided by Matplotlib;
280282
however, nbagg does not work in Jupyter Lab.
281283

282284
GUIs + Jupyter
283285
~~~~~~~~~~~~~~
284286

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
290292
see or interact with the window. It may also raise an exception.
291293

292294

0 commit comments

Comments
 (0)