matplotlib imshow():如何动画? [英] matplotlib imshow(): how to animate?

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

问题描述

我发现这个动画的精彩简短的教程:

i found this wonderful short tutorial on animation:

<一个href=\"http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/\">http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

但我不能产生相同的时尚动画imshow()的情节。
我试图取代一些行:

however i cant produce an animated imshow() plot of same fashion. I tried to replace some lines:

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
#line, = ax.plot([], [], lw=2)
a=np.random.random((5,5))
im=plt.imshow(a,interpolation='none')
# initialization function: plot the background of each frame
def init():
    im.set_data(np.random.random((5,5)))
    return im

# animation function.  This is called sequentially
def animate(i):
    a=im.get_array()
    a=a*np.exp(-0.001*i)    # exponential decay of the values
    im.set_array(a)
    return im

但我遇到错误
你能帮助我得到这个运行?
先谢谢你。
最好的,

but i run into errors can you help me get this running? thank you in advance. best,

推荐答案

您是非常密切的,但是有一个错误 - 的init 动画应返回的 iterables 包含被动画艺术家。这就是为什么在杰克的版本他们返回(这实际上是一个元组),而不是 行(这是一个单一线对象)。可悲的是文档是不明确这个!

You're very close, but there's one mistake - init and animate should return iterables containing the artists that are being animated. That's why in Jake's version they return line, (which is actually a tuple) rather than line (which is a single line object). Sadly the docs are not clear on this!

您可以修复你的版本是这样的:

You can fix your version like this:

# initialization function: plot the background of each frame
def init():
    im.set_data(np.random.random((5,5)))
    return [im]

# animation function.  This is called sequentially
def animate(i):
    a=im.get_array()
    a=a*np.exp(-0.001*i)    # exponential decay of the values
    im.set_array(a)
    return [im]

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

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