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

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

问题描述

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

i found this wonderful short tutorial on animation:

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,

推荐答案

你很接近,但有一个错误 - initanimate 应该返回 iterables 包含正在制作动画的艺术家.这就是为什么在 Jake 的版本中,他们返回 line,(实际上是一个元组)而不是 line(这是一个单行对象).遗憾的是,文档对此并不清楚!

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天全站免登陆