【问题标题】:Pyplot animation not updating continuouslyPyplot动画不连续更新
【发布时间】:2013-07-12 21:12:29
【问题描述】:

我正在尝试使用 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 的整数数组。我认为这对于动画一次处理来说不会太多。

【问题讨论】:

  • 如果增加间隔,它运行得更好吗?我会指出 10 毫秒是 100 fps。我怀疑这里的罪魁祸首是您使用的 gui 框架工作无法以这种速度重新绘制画布。也不清楚您是有问题还是只是在抱怨。
  • 更改间隔没有帮助。正如我所描述的,我的问题是什么会导致情节更新不规律;如果这是 pyplot 的问题并且不同的绘图库会更好,或者如果 pyplot 中有什么东西我可以解决这个问题。

标签: python animation matplotlib


【解决方案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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多