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.
Creates a new empty
Figureor selects an existing figure
pyplot has a notion of "The Current Figure" which can be accessed
pyplot.gcf and a notion of "The Current Axes" accessed
pyplot.gca. Almost all of the functions in
through the current
Axes (or create one) as
Matplotlib keeps a reference to all of the open figures
pyplot.subplots so that the figures will not be garbage
Figures can be closed and deregistered from
pyplot individually via
pyplot.close; all open
Figures can be closed via
For more discussion of Matplotlib's event system and integrated event loops:
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 Command prompt integration).
In this example, we create and modify a figure via an IPython prompt.
The figure displays in a QtAgg GUI window. To configure the integration
and enable interactive mode use the
In : %matplotlib
Using matplotlib backend: QtAgg
In : import matplotlib.pyplot as plt
Create a new figure window:
In : fig, ax = plt.subplots()
Add a line plot of the data to the window:
In : ln, = ax.plot(range(5))
Change the color of the line from blue to orange:
In : ln.set_color('orange')
If you wish to disable automatic redrawing of the plot:
In : plt.ioff()
If you wish to re-enable automatic redrawing of the plot:
In : plt.ion()
Enable interactive mode.
Disable interactive mode.
Return whether plots are updated after every plotting command.
Display all open figures.
Run the GUI event loop for interval seconds.
Interactive mode controls:
whether created figures are automatically shown
whether changes to artists automatically trigger re-drawing existing figures
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 be displayed immediately
figures will automatically redraw when 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()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
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
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 try to use
pyplot.ion without arranging 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.pause, or run
the GUI main loop in some other way.
Figure.show it is possible to display a figure on
the screen without starting the event loop and without being in
interactive mode. This may work (depending on the GUI toolkit) but
will likely result in a non-responsive figure.
The windows created by
pyplot have an interactive toolbar with navigation
buttons and a readout of the data values the cursor is pointing at. A number of
helpful keybindings are registered by default.
Other Python prompts#
Interactive mode works in the default Python prompt:
>>> import matplotlib.pyplot as plt
however this does not ensure that the event hook is properly installed and your figures may not be responsive. Please consult the documentation of your GUI toolkit for details.
Jupyter Notebooks / JupyterLab#
To get the interactive functionality described here, you must be
using an interactive backend. The default backend in notebooks,
the inline backend, is not.
renders the figure once and inserts a static image into the
notebook when the cell is executed. Because the images are static, they
can not be panned / zoomed, take user input, or be updated from other
To get interactive figures in the 'classic' notebook or Jupyter lab,
use the ipympl backend
(must be installed separately) which uses the ipywidget framework.
ipympl is installed use the magic:
to select and enable it.
If you only need to use the classic notebook, you can use
which uses the
backend_nbagg backend provided by Matplotlib;
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.
PyCharm, Spyder, and VSCode#
Many IDEs have built-in integration with Matplotlib, please consult their documentation for configuration details.