How to update Matplotlib graph while the program is running?

Following code

plt.figure(1) plt.subplot(211) plt.axis([0,100, 95, 4000]) plt.plot(array1,array2,'r') plt.ylabel("label") plt.xlabel("label") plt.subplot(212) plt.specgram(array3) plt.show() 

creates two beautiful diagrams. But how do you update your content without closing the window?

I need to create a window in one thread, and although the variable is updated in the main code, the window is updated using the variable.

How do you do this?

+4
source share
1 answer

There are several options: one of them is great examples using mpl examples . the second records the loops so you can understand what is happening. Here is a simple example of using the pylab.draw () function instead of show (), this is not fancy, but it will let you understand the basic things:

 import pylab import time pylab.ion() # animation on # Note the comma after line. This is placed here because # plot returns a list of lines that are drawn. line, = pylab.plot(0,1,'ro',markersize=6) pylab.axis([0,1,0,1]) line.set_xdata([1,2,3]) # update the data line.set_ydata([1,2,3]) pylab.draw() # draw the points again time.sleep(6) line1, = pylab.plot([4],[5],'g*',markersize=8) pylab.draw() for i in range(10): line.set_xdata([1,2,3]) # update the data line.set_ydata([1,2,3]) pylab.draw() # draw the points again time.sleep(1) print "done up there" line2, = pylab.plot(3,2,'b^',markersize=6) pylab.draw() time.sleep(20) 

Hope this helps.

+6
source

Source: https://habr.com/ru/post/1392378/


All Articles