8000 Backport PR #19867: Remove "Use show()" from how-to · meeseeksmachine/matplotlib@dbd0caa · GitHub
[go: up one dir, main page]

Skip to content

Commit dbd0caa

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR matplotlib#19867: Remove "Use show()" from how-to
1 parent d0934c6 commit dbd0caa

File tree

2 files changed

+27
-69
lines changed

2 files changed

+27
-69
lines changed

doc/faq/howto_faq.rst

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -436,64 +436,6 @@ the desired format::
436436
:doc:`/gallery/user_interfaces/web_application_server_sgskip` for
437437
information about running matplotlib inside of a web application.
438438

439-
.. _howto-show:
440-
441-
Use :func:`~matplotlib.pyplot.show`
442-
-----------------------------------
443-
444-
When you want to view your plots on your display,
445-
the user interface backend will need to start the GUI mainloop.
446-
This is what :func:`~matplotlib.pyplot.show` does. It tells
447-
Matplotlib to raise all of the figure windows created so far and start
448-
the mainloop. Because this mainloop is blocking by default (i.e., script
449-
execution is paused), you should only call this once per script, at the end.
450-
Script execution is resumed after the last window is closed. Therefore, if
451-
you are using Matplotlib to generate only images and do not want a user
452-
interface window, you do not need to call ``show`` (see :ref:`howto-batch`
453-
and :ref:`what-is-a-backend`).
454-
455-
.. note::
456-
Because closing a figure window unregisters it from pyplot, you must call
457-
`~matplotlib.pyplot.savefig` *before* calling ``show`` if you wish to save
458-
the figure as well as view it.
459-
460-
Whether ``show`` blocks further execution of the script or the python
461-
interpreter depends on whether Matplotlib is set to use interactive mode.
462-
In non-interactive mode (the default setting), execution is paused
463-
until the last figure window is closed. In interactive mode, the execution
464-
is not paused, which allows you to create additional figures (but the script
465-
won't finish until the last figure window is closed).
466-
467-
Because it is expensive to draw, you typically will not want Matplotlib
468-
to redraw a figure many times in a script such as the following::
469-
470-
plot([1, 2, 3]) # draw here?
471-
xlabel('time') # and here?
472-
ylabel('volts') # and here?
473-
title('a simple plot') # and here?
474-
show()
475-
476-
However, it is *possible* to force Matplotlib to draw after every command,
477-
which might be what you want when working interactively at the
478-
python console (see :ref:`mpl-shell`), but in a script you want to
479-
defer all drawing until the call to ``show``. This is especially
480-
important for complex figures that take some time to draw.
481-
:func:`~matplotlib.pyplot.show` is designed to tell Matplotlib that
482-
you're all done issuing commands and you want to draw the figure now.
483-
484-
.. note::
485-
486-
:func:`~matplotlib.pyplot.show` should typically only be called at
487-
most once per script and it should be the last line of your
488-
script. At that point, the GUI takes control of the interpreter.
489-
If you want to force a figure draw, use
490-
:func:`~matplotlib.pyplot.draw` instead.
491-
492-
.. versionadded:: v1.0.0
493-
Matplotlib 1.0.0 and 1.0.1 added support for calling ``show`` multiple times
494-
per script, and harmonized the behavior of interactive mode, across most
495-
backends.
496-
497439
.. _how-to-threads:
498440

499441
Working with threads

lib/matplotlib/pyplot.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,29 +326,45 @@ def show(*args, **kwargs):
326326
"""
327327
Display all open figures.
328328
329-
In non-interactive mode, *block* defaults to True. All figures
330-
will display and show will not return until all windows are closed.
331-
If there are no figures, return immediately.
332-
333-
In interactive mode *block* defaults to False. This will ensure
334-
that all of the figures are shown and this function immediately returns.
335-
336329
Parameters
337330
----------
338331
block : bool, optional
332+
Whether to wait for all figures to be closed before returning.
339333
340-
If `True` block and run the GUI main loop until all windows
334+
If `True` block and run the GUI main loop until all figure windows
341335
are closed.
342336
343-
If `False` ensure that all windows are displayed and return
337+
If `False` ensure that all figure windows are displayed and return
344338
immediately. In this case, you are responsible for ensuring
345339
that the event loop is running to have responsive figures.
346340
341+
Defaults to True in non-interactive mode and to False in interactive
342+
mode (see `.pyplot.isinteractive`).
343+
347344
See Also
348345
--------
349-
ion : enable interactive mode
350-
ioff : disable interactive mode
346+
ion : Enable interactive mode, which shows / updates the figure after
347+
every plotting command, so that calling ``show()`` is not necessary.
348+
ioff : Disable interactive mode.
349+
savefig : Save the figure to an image file instead of showing it on screen.
350+
351+
Notes
352+
-----
353+
**Saving figures to file and showing a window at the same time**
354+
355+
If you want an image file as well as a user interface window, use
356+
`.pyplot.savefig` before `.pyplot.show`. At the end of (a blocking)
357+
``show()`` the figure is closed and thus unregistered from pyplot. Calling
358+
`.pyplot.savefig` afterwards would save a new and thus empty figure. This
359+
limitation of command order does not apply if the show is non-blocking or
360+
if you keep a reference to the figure and use `.Figure.savefig`.
361+
362+
**Auto-show in jupyter notebooks**
351363
364+
The jupyter backends (activated via ``%matplotlib inline``,
365+
``%matplotlib notebook``, or ``%matplotlib widget``), call ``show()`` at
366+
the end of every cell by default. Thus, you usually don't have to call it
367+
explicitly there.
352368
"""
353369
_warn_if_gui_out_of_main_thread()
354370
return _backend_mod.show(*args, **kwargs)

0 commit comments

Comments
 (0)
0