Exact semantics of Matplotlib's "interactive mode" (ion(), ioff())?
Asked Answered
M

2

21

The documentation for the "interactive mode" in Matplotlib's pyplot reads:

The interactive property of the pyplot interface controls whether a figure canvas is drawn on every pyplot command. If interactive is False, then the figure state is updated on every plot command, but will only be drawn on explicit calls to draw(). When interactive is True, then every pyplot command triggers a draw.

This seems clear enough: when the interactive mode is on, one can do plot() without having to do draw(). However, doing draw() in the following code does not do anything:

from matplotlib import pyplot as pp

# Interactive mode is off by default

pp.plot([10, 20, 50])
pp.draw()

raw_input('Press enter...')  # No graph displayed?!!

(on Windows XP, Matplotlib 1.0.1).

Adding ion() at the beginning makes the figure(s) appear, while waiting for the user to type enter (which conveniently closes all the figures):

from matplotlib import pyplot as pp

ion()

pp.plot([10, 20, 50])  # No draw() is necessary

raw_input('Press enter...')  # The graph is interactive *and* the terminal responds to enter

Thus, it looks like ion() does more than just adding automatic graph updates after each plotting command, and I unfortunately can't find anything in the documentation. Another, more important problem with the latter program is that ion() makes all plot commands update the graph, which is time consuming when a single graph is updated multiple times.

So, is there a way of:

  • having the terminal wait for enter, after which all the figures are automatically closed,
  • having interactive Matplotlib graphs,
  • … without forcing the interactive mode to be on at the beginning (so as to not force auto-updates of the graphs, which could be time consuming)?
Mandorla answered 25/5, 2011 at 20:15 Comment(3)
I think you need to call pp.show() rather than pp.draw() there.Uranography
@Thomas K: the goal is to have the program wait for the user to type enter, so show() is precluded.Mandorla
As far as I know, interactive mode is the only way to achieve that, then. But I'm not an expert - you might want to ask on the matplotlib user mailing list.Uranography
M
16

Here is the summary of an interesting discussion on this subject in the Matplotlib mailing list. The executive summary is:

  • The interactive mode (activated with ion()) automates many things. In particular, pyplot.* commands automatically update on the screen the relevant axes. However, method calls on Matplotlib objects like ax.plot() (ax being an Axes object) do not normally perform automatic updates; in this case, pyplot.draw() performs the necessary update.)

  • The non-interactive mode is less convenient. draw() does not normally update the figure on screen. The fact that draw() is somewhat "inactive" in non-interactive mode is not mentioned in the current documentation, but will hopefully be included there soon.

In the mean time, more information on the interactive and non-interactive modes can be found in a current branch of Matplotlib. A better documentation for draw(), show() and friends can also be found in the same branch.

Mandorla answered 22/6, 2011 at 20:50 Comment(3)
Could you explain how to use ax in ways a novice matplotlib user would understand?Seymour
ax in an Axes object: it's essentially a box (or circle) with coordinates (a figure can thus contain many axes). In practice, Axes object are returned when you create axes in a figure. Thus, you usually get Axes with commands like: fig = pyplot.figure(); ax = fig.add_subplot(111) and fig, (ax_scatter, ax_log_hist) = pyplot.subplots(1, 2).Mandorla
@EricOLebigot the link is dead to the branchUnite
H
4

I would suggest that you follow the last comment of 'Thomas K'. I remember a similar question on the mailing list, but I couldn't find it after several minutes of searching. Sorry.

I had also this problem and the better easier way for me was/is to use ipython --pylab. I have a much older version of matplotlib installed which have some problems with ion(). Beside this, matplotlib had also some problems with draw() on Windows. Maybe it was fixed in the last versions.

p.s.: Sorry that I couldn't helped you really well.

Best regards.

Hairtail answered 25/5, 2011 at 23:43 Comment(2)
I was not able to find any relevant answer on the matplotlib-user mailing list either. I just posted a new message there. Thank you for the suggestion.Mandorla
PS: Here is the thread: old.nabble.com/…. I will summarize the result of the interesting discussion we had there as soon as I get a confirmation about some points.Mandorla

© 2022 - 2024 — McMap. All rights reserved.