Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting lines without blocking execution

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
like image 290
Abruzzo Forte e Gentile Avatar asked Feb 22 '10 12:02

Abruzzo Forte e Gentile


People also ask

What is PLT show block false?

plt.show(block=False) #this creates an empty frozen window.

Which module is used for plotting?

While Matplotlib contains many modules that provide different plotting functionality, the most commonly used module is pyplot.

How do you clear old plots in python?

Use plt. clf() to clear a plot Call plt. clf() to clear the current figure of plt .

How do I stop a plot showing in Matplotlib?

We can simply save plots generated from Matplotlib using savefig() and imsave() methods. If we are in interactive mode, the plot might get displayed. To avoid the display of plot we use close() and ioff() methods.


2 Answers

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

like image 63
Stefan Avatar answered Oct 17 '22 21:10

Stefan


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.).

like image 30
Eric O Lebigot Avatar answered Oct 17 '22 21:10

Eric O Lebigot