【问题标题】:How can I speed up an animation?如何加快动画速度?
【发布时间】:2011-06-27 13:09:47
【问题描述】:

我正在尝试为我的爪子数据创建一个 Matplotlib 动画,您可以在其中看到the entire pressure plate over time 上的压力分布(256x64 传感器,250 帧)。

我找到了一个working example on Matplotlib's own site 并设法让它处理我自己的数据。但是“动画”非常慢,我不知道如何加快速度。

这是一个 gif Joe Kington made in another answer 的示例,它与显示速度有关。考虑到测量是在 125 Hz 下进行的,这使得测量看起来非常缓慢。如果它以 30-60 fps 的速度运行,它可能会在 4 或 8 秒内运行,而不是当前的 20+。

我不介意使用完成工作所需的任何工具,只要有一些好的文档来弄清楚如何做。

所以我的问题是:如何加快这些动画的速度?

I've implemented Ignacio's suggestion 放入 t.Start(1),但是只有当图形这么大时它才会“正常”运行:

class PlotFigure(Frame):
    """ This class draws a window and updates it with data from DataCollect
    """
    def __init__(self):
        Frame.__init__(self, None, -1, "Test embedded wxFigure")
        #Varying the size of Figure has a big influence on the speed            
        self.fig = Figure((3,3), 75) 
        self.canvas = FigureCanvasWxAgg(self, -1, self.fig)
        EVT_TIMER(self, TIMER_ID, self.onTimer)

    def init_plot_data(self):
        self.datagen = DataCollect(array3d)
        self.axes = self.fig.add_subplot(111)
        self.axes.imshow(self.datagen.next().T)

    def onTimer(self, evt):
        self.data = self.datagen.next()
        self.axes.imshow(self.datagen.next().T)
        self.canvas.draw()

当我在动画期间调整窗口大小时,它会立即减速到爬行。这让我怀疑延迟不是放缓的唯一原因。 还有其他建议吗?如果你好奇,here's a link to one of the ASCII files.

【问题讨论】:

  • 创建 png 文件并将它们拼接成动画 gif 怎么样?
  • 这似乎不是一个很好的长期解决方案@ralu,因为我还必须将所有 gif 文件与数据一起存储。每次临床医生想要查看测量结果时生成 gif 图像似乎也很麻烦......我更愿意学习如何正确地做到这一点

标签: python animation


【解决方案1】:

传递给wx.Timer.Start() 的值是以毫秒为单位的触发率。传递一个较小的值。

【讨论】:

  • 它在 Matplotlib 示例 @Ignacio 中工作,所以我必须弄清楚如何用我自己的数据复制它
  • 我设法调整了我的代码,所以我必须将间隔设置为 1。但它仍然不是真正的性能......也许还有其他建议吗?
  • 此时,其他因素正在成为瓶颈,例如 CPU、显卡或内存(当然还有代码本身)。尝试使用较小的图像对其进行测试。
  • 嗯,它在 Matplotlib 中的示例运行得很快,但在我的数组中运行速度变慢。当绘图表面变大时,它也会变慢。鉴于我电脑的规格,我怀疑我的问题出在我的代码中:\
【解决方案2】:

使用 Profiler 查找根本原因,作为最后的手段,跳帧可能也很有用。

或者切换到其他解决方案,例如 Double Buffering using Device ContextsPyOpenGL...

【讨论】:

    【解决方案3】:

    我找到了Joe Kington's answer that mentioned using Glumpy。起初我无法让它处理我自己的数据,但with some help on chat 我们设法弄清楚如何调整one of the Matplotlib examples that come with Glumpy 以处理我的数据。

    import numpy, glumpy
    from glumpy.pylab import *
    
    window = glumpy.Window(256,64)
    Z = data.astype(numpy.float32)
    
    t0, frames, t = 0,0,0
    fig = plt.figure(figsize=(7,7))
    ax = plt.subplot(111)
    ax = imshow(Z[:,:,0], origin='lower', interpolation='bilinear')
    show()
    window = glumpy.active_window()
    
    @window.event
    def on_idle(dt):    
        global Z, t0, frames, t
        
        t += dt
        frames = frames + 1
        if frames > 248:
            fps = float(frames)/(t-t0)
            print 'FPS: %.2f (%d frames in %.2f seconds)' % (fps, frames, t-t0)
            frames,t0 = 0, t
        
        for image, axis, alpha in items:
            image.data[...] = Z[:,:,frames]
            image.update()
        window.draw()
    
    window.mainloop()
    

    最终结果可以在这里看到,不管我把窗口做得多大,它都会以非常稳定的 58+ fps 运行。所以我必须说,我对最终结果非常满意!

    【讨论】:

    • 我收到Traceback (most recent call last): File "main.py", line 4, in <module> window = glumpy.Window(256,64) File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/glumpy/window/backend_glut.py", line 95, in __init__ window.Window.__init__( self, size, position, title ) File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/glumpy/window/window.py", line 156, in __init__ self._width, self._height = size TypeError: 'int' object is not iterable
    • 也许他们改变了功能。初始化读取:def __init__( self, size=(640,480), position=(0,0), title=None ): 所以尝试将glumpy.Window(256,64) 更改为glumpy.Window(size=(256,64))
    猜你喜欢
    • 2010-12-08
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多