【问题标题】:Updating a plot in python in real time实时更新python中的绘图
【发布时间】:2012-09-22 17:23:06
【问题描述】:

我有一个 python 代码,我在其中计算大量参数值的数量,然后将数量绘制为参数的函数。这是一个例子

t = np.linspace(1,100,10000)
q = np.zeros(10000)
for i in np.arange(10000)
   q[i] = func(t[i])
plt.plot(t,q)
plt.show()

但是,我希望绘图能够动态更新,以便每次计算 q 数组的新元素时将其添加到绘图中。我该怎么做?

【问题讨论】:

标签: python dynamic matplotlib


【解决方案1】:
from pylab import *

import time

ion()

tstart = time.time()               # for profiling
x = arange(0,2*pi,0.01)            # x-array
line, = plot(x,sin(x))

for i in arange(1,200):
    line.set_ydata(sin(x+i/10.0))  # update the data
    draw()                         # redraw the canvas


print 'FPS:' , 200/(time.time()-tstart)

从我放入 cmets 的帖子中撕下...

【讨论】:

  • 我真的不明白如何在我的情况下实现这一点,因为在这种情况下,它会更新 line.set_ydata 行中的完整数组,而在我的情况下,每次数组的新元素是已更新。
  • @SthitadhiRoy 查看this answer。它可能会对你有所帮助。
猜你喜欢
  • 2016-02-01
  • 1970-01-01
  • 2015-05-26
  • 2014-08-20
  • 2021-10-25
  • 2014-09-07
  • 1970-01-01
  • 2013-03-22
  • 2019-02-13
相关资源
最近更新 更多