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

Skip to content

Commit 07fd38f

Browse files
authored
Merge pull request #19867 from timhoffm/doc-show
DOC: Remove "Use show()" from how-to
2 parents 007561d + 202a5f9 commit 07fd38f

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
@@ -323,29 +323,45 @@ def show(*args, **kwargs):
323323
"""
324324
Display all open figures.
325325
326-
In non-interactive mode, *block* defaults to True. All figures
327-
will display and show will not return until all windows are closed.
328-
If there are no figures, return immediately.
329-
330-
In interactive mode *block* defaults to False. This will ensure
331-
that all of the figures are shown and this function immediately returns.
332-
333326
Parameters
334327
----------
335328
block : bool, optional
329+
Whether to wait for all figures to be closed before returning.
336330
337-
If `True` block and run the GUI main loop until all windows
331+
If `True` block and run the GUI main loop until all figure windows
338332
are closed.
339333
340-
If `False` ensure that all windows are displayed and return
334+
If `False` ensure that all figure windows are displayed and return
341335
immediately. In this case, you are responsible for ensuring
342336
that the event loop is running to have responsive figures.
343337
338+
Defaults to True in non-interactive mode and to False in interactive
339+
mode (see `.pyplot.isinteractive`).
340+
344341
See Also
345342
--------
346-
ion : enable interactive mode
347-
ioff : disable interactive mode
343+
ion : Enable interactive mode, which shows / updates the figure after
344+
every plotting command, so that calling ``show()`` is not necessary.
345+
ioff : Disable interactive mode.
346+
savefig : Save the figure to an image file instead of showing it on screen.
347+
348+
Notes
349+
-----
350+
**Saving figures to file and showing a window at the same time**
351+
352+
If you want an image file as well as a user interface window, use
353+
`.pyplot.savefig` before `.pyplot.show`. At the end of (a blocking)
354+
``show()`` the figure is closed and thus unregistered from pyplot. Calling
355+
`.pyplot.savefig` afterwards would save a new and thus empty figure. This
356+
limitation of command order does not apply if the show is non-blocking or
357+
if you keep a reference to the figure and use `.Figure.savefig`.
358+
359+
**Auto-show in jupyter notebooks**
348360
361+
The jupyter backends (activated via ``%matplotlib inline``,
362+
``%matplotlib notebook``, or ``%matplotlib widget``), call ``show()`` at
363+
the end of every cell by default. Thus, you usually don't have to call it
364+
explicitly there.
349365
"""
350366
_warn_if_gui_out_of_main_thread()
351367
return _backend_mod.show(*args, **kwargs)

0 commit comments

Comments
 (0)