如何加快动画速度? [英] How can I speed up an animation?

查看:54
本文介绍了如何加快动画速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的爪子数据创建一个 Matplotlib 动画,您可以在其中看到 来放入 t.Start(1),但它只能运行 '体面'当图这么大时:

class PlotFigure(Frame):""" 此类绘制一个窗口并使用来自 DataCollect 的数据对其进行更新"""def __init__(self):Frame.__init__(self, None, -1, "Test Embedded wxFigure")#改变图的大小对速度有很大影响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()

当我在动画过程中调整窗口大小时,它会立即减速到爬行.这让我怀疑延迟并不是速度变慢的唯一原因.还有其他建议吗?如果您好奇,这里有一个链接到其中一个 ASCII 文件.

解决方案

我发现 Joe Kington 的回答提到使用 Glumpy.起初我无法让它处理我自己的数据,但是在聊天方面的一些帮助我们设法弄清楚如何适应 其中一个Glumpy 附带的 Matplotlib 示例 用于处理我的数据.

import numpy, glumpy从 glumpy.pylab 导入 *window = glumpy.Window(256,64)Z = data.astype(numpy.float32)t0,帧,t = 0,0,0fig = plt.figure(figsize=(7,7))ax = plt.subplot(111)ax = imshow(Z[:,:,0], origin='lower',interpolation='bilinear')展示()window = glumpy.active_window()@window.eventdef on_idle(dt):全局 Z, t0, 帧, tt += dt帧数 = 帧数 + 1如果帧 >248:fps = 浮动(帧)/(t-t0)打印 'FPS: %.2f (%d 帧在 %.2f 秒内)' % (fps, 帧, t-t0)帧,t0 = 0, t对于项目中的图像、轴、alpha:image.data[...] = Z[:,:,frames]图像更新()window.draw()window.mainloop()

最终结果可以在这里看到,无论我制作多大的窗口,它都会以非常稳定的 58+ fps 运行.所以我必须说,我对最终结果非常满意!

I'm trying to create a Matplotlib animation of my paw data, where you can see the pressure distribution on the entire pressure plate over time (256x64 sensors for 250 frames).

I found a working example on Matplotlib's own site and managed to get it working on my own data. However the 'animation' is awfully slow and I have no idea how to speed it up.

Here's an example of a gif Joe Kington made in another answer, which is about the speed with which it gets displayed. Considering the measurements are done at 125 Hz, this makes the measurements look awfully slow. If it ran at 30-60 fps, it could be run in 4 or 8 seconds rather than the current 20+.

I don't mind using whatever tool I need to get the job done, as long as there's some good documentation to figure out how to do it.

So my question is: how can I speed up these animations?

I've implemented Ignacio's suggestion to put in t.Start(1), however it only runs 'decently' when the Figure is this large:

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()

When I resize the window during the animation, it immediately slows down to a crawl. Which makes me suspect the delay isn't the only cause of the slow down. So any other suggestions? In case you're curious, here's a link to one of the ASCII files.

解决方案

I found Joe Kington's answer that mentioned using Glumpy instead. At first I couldn't get it to work on my own data, but with some help on chat we managed to figure out how to adapt one of the Matplotlib examples that come with Glumpy to work on my data.

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()

The end result can be seen here, it doesn't matter how large I make the window, it will run at a very steady 58+ fps. So I must say, I'm very pleased with the end result!

这篇关于如何加快动画速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆