Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Removed idiomatic constructs from interactive figures docs#17471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
9ada3fe
28c3b4f
05241d1
1f39173
bd5d63a
e6411d4
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -9,22 +9,21 @@ | ||
.. toctree:: | ||
When working with data, interactivity can be invaluable. The pan/zoom and | ||
mouse-location tools built into the Matplotlib GUI windows are often sufficient, but | ||
you can also use the event system to build customized data exploration tools. | ||
Matplotlib ships with :ref:`backends <what-is-a-backend>` binding to | ||
several GUI toolkits (Qt, Tk, Wx, GTK, macOS, JavaScript) and third party | ||
packages provide bindings to `kivy | ||
<https://github.com/kivy-garden/garden.matplotlib>`__ and `Jupyter Lab | ||
<https://github.com/matplotlib/ipympl>`__. For the figures to be responsive to | ||
mouse, keyboard, and paint events,the GUI event loopneedsto be integrated | ||
with an interactive prompt. We recommend using IPython (see :ref:`below <ipython-pylab>`). | ||
The `.pyplot` module provides functions for explicitly creating figures | ||
that include interactive tools, a toolbar, a tool-tip, and | ||
:ref:`key bindings <key-event-handling>`: | ||
`.pyplot.figure` | ||
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 | ||
through `.pyplot.gcf` and a notion of "The Current Axes" accessed | ||
through `.pyplot.gca`. Almost all of the functions in `.pyplot` pass | ||
through the current `.Figure` / `.axes.Axes` (or create one) as | ||
appropriate. | ||
Matplotlib keeps a reference to all of the open figures | ||
created via `pyplot.figure` or `pyplot.subplots` so that the figures will not be garbage | ||
collected. `.Figure`\s can be closed and deregistered from `.pyplot` individually via | ||
`.pyplot.close`; all open `.Figure`\s can be closed via ``plt.close('all')``. | ||
For more discussion of Matplotlib's event system and integrated event loops, please read: | ||
.. toctree:: | ||
:maxdepth: 1 | ||
@@ -57,64 +58,58 @@ IPython integration | ||
=================== | ||
We recommend using IPython for an interactive shell. In addition to | ||
all of its features (improved tab-completion, magics, multiline editing, etc), | ||
it also ensures that the GUI toolkit event loop is properly integrated | ||
with the command line (see :ref:`cp_integration`). | ||
In this example, we create and modify a figure via an IPython prompt. | ||
The figure displays in a Qt5Agg GUI window. To configure the integration | ||
and enable :ref:`interactive mode <controlling-interactive>` use the | ||
``%matplotlib`` magic: | ||
.. highlight:: ipython | ||
:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think it is helpful to identify that we are in an IPython session, we should not assume that our readers well recognize the prompt as IPython from the style. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We say that it's IPython in the narrative, but I'm fine putting this line back in. I just don't want them getting tripped up on all the details of your particular Ipython install | ||
In [1]: %matplotlib | ||
Using matplotlib backend: Qt5Agg | ||
In [2]: import matplotlib.pyplot as plt | ||
Create a new figure window: | ||
:: | ||
In [3]: fig, ax = plt.subplots() | ||
Add a line plot of the data to the window: | ||
:: | ||
In [4]: ln, = ax.plot(range(5)) | ||
Change the color of the line from blue to orange: | ||
:: | ||
In [5]: ln.set_color('orange') | ||
If you wish to disable automatic redrawing of the plot: | ||
:: | ||
In [6]: plt.ioff() | ||
If you wish to re-enable automatic redrawing of the plot: | ||
:: | ||
In [7]: plt.ion() | ||
In recent versions of ``Matplotlib`` and ``IPython``, it is | ||
sufficient to import `matplotlib.pyplot` and call `.pyplot.ion`. | ||
Using the``%``magic is guaranteed to workin all versions of Matplotlib and IPython. | ||
.. highlight:: python | ||
@@ -146,45 +141,43 @@ Interactive mode controls: | ||
- whether created figures are automatically shown | ||
- whether changes to artists automatically trigger re-drawing existing figures | ||
- when `.pyplot.show()` returns if given no arguments: immediately, or after all of the figures have been closed | ||
If in interactive mode: | ||
- newly created figures will bedisplayed immediately | ||
- figures will automatically redrawwhen elements are changed | ||
-`pyplot.show()` displays the figures and immediately returns | ||
If not in interactive mode: | ||
- newly created figures and changes to figures are not displayed until | ||
* `.pyplot.show()` is called | ||
* `.pyplot.pause()` is called | ||
* `.FigureCanvasBase.flush_events()` is called | ||
- `pyplot.show()` runs the GUI event loop and does not return until all the plot windows are closed | ||
If you are in non-interactive mode (or created figures while in | ||
non-interactive mode) you may need to explicitly call `.pyplot.show` | ||
to display the windows on your screen. If you only want to run the | ||
GUI event loop for a fixed amount of time, you can use `.pyplot.pause`. | ||
This will block the progress of your code as if you had called | ||
`time.sleep`, ensure the current window is shown and re-drawn if needed, | ||
and run the GUI event loop for the specified period of time. | ||
The GUI event loop being integrated with your command prompt and | ||
the figures being in interactive mode are independent of each other. | ||
If you use `pyplot.ion` but have not arranged for the event loop integration, | ||
your figures will appear but will not be interactive while the prompt is waiting for input. | ||
You will not be able to pan/zoom and the figure may not even render | ||
(the window might appear black, transparent, or as a snapshot of the | ||
desktop under it). Conversely, if you configure the event loop | ||
integration, displayed figures will be responsive while waiting for input | ||
at the prompt, regardless of pyplot's "interactive mode". | ||
No matter what combination of interactive mode setting and event loop integration, | ||
figures will be responsive if you use ``pyplot.show(block=True)``, `.pyplot.pause`, or run | ||
the GUI main loop in some other way. | ||
.. warning:: | ||
@@ -201,7 +194,7 @@ Default UI | ||
The windows created by :mod:`~.pyplot` have an interactive toolbar with navigation | ||
buttons and a readout ofthe data valuesthe cursor ispointing at. A number of | ||
helpful keybindings are registered by default. | ||
@@ -240,8 +233,7 @@ Preserve aspect ratio hold **CONTROL** when panning/zooming with mo | ||
Other Python prompts | ||
==================== | ||
Interactive mode works in the default Python prompt: | ||
tacaswell marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
.. sourcecode:: pycon | ||
@@ -265,11 +257,11 @@ Jupyter Notebooks / Lab | ||
using an interactive backend. The default backend in notebooks, | ||
the inline backend, is not. `~ipykernel.pylab.backend_inline` | ||
renders the figure once and inserts a static image into the | ||
notebook when the cell is executed.Because theimages are static, they | ||
cannot be panned / zoomed, take user input, or be updated from other | ||
cells. | ||
To get interactive figures in the 'classic' notebook orJupyter lab, | ||
use the `ipympl <https://github.com/matplotlib/ipympl>`__ backend | ||
(must be installed separately) which uses the **ipywidget** framework. | ||
If ``ipympl`` is installed use the magic: | ||
@@ -280,25 +272,24 @@ If ``ipympl`` is installed use the magic: | ||
to select and enable it. | ||
If you only need to use the classic notebook, you can use | ||
.. sourcecode:: ipython | ||
%matplotlib notebook | ||
story645 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
which uses the `.backend_nbagg` backendprovided byMatplotlib; | ||
however, nbagg does not work in Jupyter Lab. | ||
GUIs +Jupyter | ||
~~~~~~~~~~~~~~ | ||
You can also use one of the non-``ipympl`` GUI backends in a Jupyter Notebook. | ||
If you are running your Jupyter kernel locally, the GUI window will spawn on | ||
your desktop adjacent to your web browser. If you run your notebook on a remote server, | ||
the kernel will try to open the GUI window on the remote computer. Unless you have | ||
arranged to forward the xserver back to your desktop, you will not be able to | ||
see or interact with the window. It may also raise an exception. | ||