17

I am using matplotlib to draw charts and graphs.

When I plot the chart using the command show() my code blocks at this command.

I would like to refresh my list of values with new data , and than refresh the image on the background. How to do that without closing each time the window with the graph? Below is the code I am using

import pylab
a = (1,2,3,4)
pylab.plot(a)
pylab.show() # blocks here
user229044
  • 232,980
  • 40
  • 330
  • 338
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
  • What happens if you do not call pylab.show()? – telliott99 Feb 22 '10 at 12:58
  • You might want to mark one of the answers as "accepted" (green check mark), if it solved your problem. Or maybe adding your own answer, if you found something else. Or even possibly a comment, to let everybody know that none of the answers worked. :) – Eric O. Lebigot May 08 '10 at 10:08
  • possible duplicate of [Is there a way to detach matplotlib plots so that the computation can continue?](http://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue) – Wernight Mar 19 '13 at 12:08

4 Answers4

12

In IPython started with -pylab it should not block.

Otherwise: With ion() you turn the interactive mode on. show() does not block your system anymore. Every draw() or plot(x, y) updated your plot.

ioff() turns interactive mode off. Useful if you add lots of data and don't want to update every little detail.

See also: http://www.scipy.org/Cookbook/Matplotlib/Animations

Stefan
  • 1,246
  • 1
  • 9
  • 13
  • If using modern IPython (Jupyter), cli `-pylab` flag has been deprecated in favor of the per-notebook `%pylab` command. – tyleha Dec 08 '15 at 00:28
3

If you are not using the IPython shell but instead running a program, you probably want to do:

pyplot.draw()

after a plot(), possibly followed by

raw_input("Press enter when done...")

so as to wait for the user before plotting something else.

If you do pyplot.ion() at the beginning of your program, doing draw() can often even be skipped.

pyplot.show() is actually an infinite loop that handles events in the main plotting window (such as zooming, panning, etc.).

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
0

On MacOS X i had the problem that unblocking only produced a white screen. In the end @tyleha's suggestion using %pylab directly in the note book helped. In fact it's suggested when using the deprecated the -pylab flag:

bash:~/Projects/plyground $ python -m IPython notebook -pylab
WARNING: `-pylab` flag has been deprecated.
Use `--matplotlib <backend>` and import pylab manually.
[E 21:09:05.446 NotebookApp] Support for specifying --pylab on the command line has been removed.
[E 21:09:05.447 NotebookApp] Please use `%pylab` or `%matplotlib` in the notebook itself.
lkiraly
  • 56
  • 1
  • 7
-1

This works by invoking Ipython with the -wthread (or the -pylab) option. It will not block on show anymore.

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90