在python中实时更新情节 [英] Updating a plot in python in real time

查看:166
本文介绍了在python中实时更新情节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然而,我希望这个情节能够动态更新,这样每次新元素 q 数组被计算,它被添加到绘图。如何做到这一点?

解决方案

 

从pylab import *

import time

ion()

tstart = time.time()#用于剖析
x = arange(0,2 * pi,0.01)#x对于我在一个(1 200)中的i,$ a
line,= plot(x,sin(x))


line.set_ydata(sin(x + i / 10.0) )#更新数据
draw()#重绘画布


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

从我发表的帖子中删除...


I have a python code in which I calculate a quantity for a large number of values of a parameter and then plot the quantity as a function of a parameter. Here is an example

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

However I want that the plot to get dynamically updated such that every time a new element of the q array is calculated it is added to the plot. How can I do that?

解决方案

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)

ripped from the post i put in the comments ...

这篇关于在python中实时更新情节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆