Pyplot动画不会持续更新

时间:2013-07-12 21:12:30

标签: python animation matplotlib

我正在尝试使用pyplot动画实时显示CCD相机捕获的帧。我写了一个简短的python脚本只是为了测试它,虽然它的工作原理,它的确如此不稳定。它将快速更新十几个动画帧,然后暂停一秒,然后再次更新,然后再次暂停,依此类推。我想让它连续顺利地更新情节,但我不确定我哪里出错了。

我知道它不是调用相机帧缓冲区的部分;我测试了只是在一个循环中调用它并且它从未减慢,所以我认为它在动画帧的实际处理中的某处。

我的代码如下:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import Pixis100
import time

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)    

# Create ccd object
ccd = Pixis100.device()
ccd.snapshot()
ccd.focusStart()
# focusStart() tells the camera to start putting captured frames into the buffer

line, = ax.plot(ccd.getFrame()[:,2:].sum(axis=0)[::-1],'b-')
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame

# animation function
def update(data):    
    line.set_ydata(data)
    return line,

def data_gen():
    while True: yield ccd.getFrame()[:,2:].sum(axis=0)[::-1]

# call the animator
anim = animation.FuncAnimation(fig, update, data_gen,interval=10,blit=True)

plt.show()
ccd.focusStop()
# focusStop() just tells the camera to stop capturing frames

作为参考,ccd.getFrame()[:,2:]。sum(axis = 0)[:: - 1]返回一个1x1338的整数数组。我不认为这对动画一次处理来说太过分了。

1 个答案:

答案 0 :(得分:1)

问题不在animation,以下工作正常:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import time

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)    
ax.set_xlim([0, 2 *np.pi])
ax.set_ylim([-1, 1])

th = np.linspace(0, 2 * np.pi, 1000)

line, = ax.plot([],[],'b-', animated=True)
line.set_xdata(th)
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame

# animation function
def update(data):    
    line.set_ydata(data)

    return line,

def data_gen():
    t = 0
    while True:
        t +=1
        yield np.sin(th + t * np.pi/100)

# call the animator
anim = animation.FuncAnimation(fig, update, data_gen, interval=10, blit=True)
plt.show()

波动性可能来自您的帧抓取器,您正在进行的计算,或者gui在主线程上获得足够时间重新绘制的问题。见time.sleep() required to keep QThread responsive?