python - 如何在程序运行时更新 Matplotlib 绘图?

标签 python 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() 

创建两个漂亮的图表。但是如何在不关闭窗口的情况下更新其内容呢?

我需要在一个线程中创建窗口,并且在主代码中更新变量时,使用该变量更新窗口。

你会怎么做?

最佳答案

有几个选项: 一个是使用 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/

相关文章:

python - 如何加速 matplotlib、子图绘图/绘图和保存?

python - 具有给定精度的 float 范围

python - 安装pywin32时出现问题

python - "TypeError"在 Python 中使用 matplotlib 制作散点图

python - 在 matplotlib 3d 散点图中更改数据点的颜色并通过按键将其删除

numpy - 自定义 seaborn 联合图中的轴标签

python - 根据表中值的数量删除 Pandas 中的 DataFrame 行

python - 如何 Python 从包含分号的配置文件中打印一行?

Python 字典 : Prevent previous value not overwritten by the new value

python - 如何确定 matplotlib 轴是否已使用 axes.axis ('off' 关闭)?