Skip to content

Commit 261de59

Browse files
committed
faq/usage: added a section about interactive vs non-interactive modes
1 parent f28a37f commit 261de59

File tree

1 file changed

+137
-4
lines changed

1 file changed

+137
-4
lines changed

doc/faq/usage_faq.rst

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,10 @@ complicated applications, this explicitness and clarity becomes
146146
increasingly valuable, and the richer and more complete object-oriented
147147
interface will likely make the program easier to write and maintain.
148148

149-
Backends
150-
========
151-
152149
.. _what-is-a-backend:
153150

154151
What is a backend?
155-
------------------
152+
==================
156153

157154
A lot of documentation on the website and in the mailing lists refers
158155
to the "backend" and many new users are confused by this term.
@@ -290,3 +287,139 @@ macosx Cocoa rendering in OSX windows
290287
.. _pyFLTK: http://pyfltk.sourceforge.net
291288

292289

290+
291+
.. _interactive-mode:
292+
293+
What is interactive mode?
294+
===================================
295+
296+
Use of an interactive backend (see :ref:`what-is-a-backend`)
297+
permits--but does not by itself require or ensure--plotting
298+
to the screen. Whether and when plotting to the screen occurs,
299+
and whether a script or shell session continues after a plot
300+
is drawn on the screen, depends on the functions and methods
301+
that are called, and on a state variable that determines whether
302+
matplotlib is in "interactive mode". The default Boolean value is set
303+
by the :file:`matplotlibrc` file, and may be customized like any other
304+
configuration parameter (see :ref:`customizing-matplotlib`). It
305+
may also be set via :func:`matplotlib.interactive`, and its
306+
value may be queried via :func:`matplotlib.is_interactive`. Turning
307+
interactive mode on and off in the middle of a stream of plotting
308+
commands, whether in a script or in a shell, is rarely needed
309+
and potentially confusing, so in the following we will assume all
310+
plotting is done with interactive mode either on or off.
311+
312+
.. note::
313+
Major changes related to interactivity, and in particular the
314+
role and behavior of :func:`~matplotlib.pyplot.show`, were made in the
315+
transition to matplotlib version 1.0, and bugs were fixed in
316+
1.0.1. Here we describe the version 1.0.1 behavior for the
317+
primary interactive backends, with the partial exception of
318+
*macosx*.
319+
320+
Interactive mode may also be turned on via :func:`matplotlib.pyplot.ion`,
321+
and turned off via :func:`matplotlib.pyplot.ioff`.
322+
323+
Interactive example
324+
--------------------
325+
326+
From an ordinary python prompt, or after invoking ipython with no options,
327+
try this::
328+
329+
import matplotlib.pyplot as plt
330+
plt.ion()
331+
plt.plot([1.6, 2.7])
332+
333+
Assuming you are running version 1.0.1 or higher, and you have
334+
an interactive backend installed and selected by default, you should
335+
see a plot, and your terminal prompt should also be active; you
336+
can type additional commands such as::
337+
338+
plt.title("interactive test")
339+
plt.xlabel("index")
340+
341+
and you will see the plot being updated after each line. This is
342+
because you are in interactive mode *and* you are using pyplot
343+
functions. Now try an alternative method of modifying the
344+
plot. Get a
345+
reference to the :class:`~matplotlib.axes.Axes` instance, and
346+
call a method of that instance::
347+
348+
ax = plt.gca()
349+
ax.plot([3.1, 2.2])
350+
351+
Nothing changed, because the Axes methods do not include an
352+
automatic call to :func:`~matplotlib.pyplot.draw_if_interactive`;
353+
that call is added by the pyplot functions. If you are using
354+
methods, then when you want to update the plot on the screen,
355+
you need to call :func:`~matplotlib.pyplot.draw`::
356+
357+
plt.draw()
358+
359+
Now you should see the new line added to the plot.
360+
361+
Non-interactive example
362+
-----------------------
363+
364+
Start a fresh session as in the previous example, but now
365+
turn interactive mode off::
366+
367+
import matplotlib.pyplot as plt
368+
plt.ioff()
369+
plt.plot([1.6, 2.7])
370+
371+
Nothing happened--or at least nothing has shown up on the
372+
screen (unless you are using *macosx* backend, which is
373+
anomalous). To make the plot appear, you need to do this::
374+
375+
plt.show()
376+
377+
Now you see the plot, but your terminal command line is
378+
unresponsive; the :func:`show()` command *blocks* the input
379+
of additional commands until you manually kill the plot
380+
window.
381+
382+
What good is this--being forced to use a blocking function?
383+
Suppose you need a script that plots the contents of a file
384+
to the screen. You want to look at that plot, and then end
385+
the script. Without some blocking command such as show(), the
386+
script would flash up the plot and then end immediately,
387+
leaving nothing on the screen.
388+
389+
In addition, non-interactive mode delays all drawing until
390+
show() is called; this is more efficient than redrawing
391+
the plot each time a line in the script adds a new feature.
392+
393+
Prior to version 1.0, show() generally could not be called
394+
more than once in a single script (although sometimes one
395+
could get away with it); for version 1.0.1 and above, this
396+
restriction is lifted, so one can write a script like this::
397+
398+
import numpy as np
399+
import matplotlib.pyplot as plt
400+
plt.ioff()
401+
for i in range(3):
402+
plt.plot(np.random.rand(10))
403+
plt.show()
404+
405+
which makes three plots, one at a time.
406+
407+
Summary
408+
-------
409+
410+
In interactive mode, pyplot functions automatically draw
411+
to the screen.
412+
413+
When plotting interactively, if using
414+
object method calls in addition to pyplot functions, then
415+
call :func:`~matplotlib.pyplot.draw` whenever you want to
416+
refresh the plot.
417+
418+
Use non-interactive mode in scripts in which you want to
419+
generate one or more figures and display them before ending
420+
or generating a new set of figures. In that case, use
421+
:func:`~matplotlib.pyplot.show` to display the figure(s) and
422+
to block execution until you have manually destroyed them.
423+
424+
425+

0 commit comments

Comments
 (0)