2012-01-22 18 views
4

下面的代碼如何在程序運行時更新Matplotlib圖?

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

創建了兩個漂亮的圖表。但是如何在不關閉窗口的情況下更新其內容?

我需要在一個線程中創建窗口,並且在主代碼中正在更新一個變量時,窗口正在使用該變量進行更新。

你會怎麼做?

回答

6

有幾個選項: 其中一個是使用mpl examples的很好的例子。 第二個是寫你的自我循環,所以你可以理解發生了什麼。 下面是使用pylab.draw()函數,而不是秀(),它是不是看上一個簡單的例子,但它可以讓你瞭解基本的東西:

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) 

我希望這有助於。

相關問題