下面的代码
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()
创建两个漂亮的图表。但是如何在不关闭窗口的情况下更新其内容呢?
我需要在一个线程中创建窗口,并且在主代码中更新变量时,使用该变量更新窗口。
你会怎么做?
最佳答案
有几个选项: 一个是使用 mpl examples 的好例子。 第二个是自己编写循环,这样您就可以了解发生了什么。 这是一个使用 pylab.draw() 函数而不是 show() 的简单示例,它并不花哨,但可以让您了解基本内容:
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)
希望对您有所帮助。
关于python - 如何在程序运行时更新 Matplotlib 绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8965055/