Updating a plot in python in real time

筅森魡賤 提交于 2019-12-12 03:57:48

问题


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?


回答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)

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



来源:https://stackoverflow.com/questions/12680427/updating-a-plot-in-python-in-real-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!