Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyplot动画未持续更新_Python_Animation_Matplotlib - Fatal编程技术网

Python Pyplot动画未持续更新

Python Pyplot动画未持续更新,python,animation,matplotlib,Python,Animation,Matplotlib,我正在尝试使用pyplot动画实时显示CCD相机捕获的帧。我编写了一个简短的python脚本来测试这一点,虽然它可以工作,但它的工作方式却不稳定。它将快速更新十几个动画帧,然后暂停一秒钟,然后再次更新,然后再次暂停,依此类推。我只想让它不断地、顺利地更新情节,但我不确定哪里出了问题 我知道这不是它调用相机帧缓冲区的部分;我测试了在循环中调用它,它从来没有减慢,所以我认为它在动画帧的实际处理中的某个地方 我的代码如下: import numpy as np from matplotlib impo

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

问题不在
动画中,以下工作很好:

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在主线程上获得足够时间重新绘制的问题。请参见

如果增加间隔,是否运行得更好?我要指出的是,10毫秒等于100帧每秒。我怀疑这里的罪魁祸首是您使用的gui框架无法以这种速度重新绘制画布。也不清楚你是否有问题或只是在抱怨。改变时间间隔没有帮助。我的问题是什么会导致情节不稳定地更新,正如我所描述的;如果是pyplot的问题,另一个绘图库会更好地工作,或者如果pyplot中有一些东西我可以做来解决这个问题。