matplotlib imshow():如何制作动画?

2024-02-23

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

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

但是我无法制作同样时尚的动画 imshow() 情节。 我尝试替换一些行:

# 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

但我遇到了错误 你能帮我让它运行吗? 先感谢您。 最好的,


你已经很接近了,但有一个错误 -init and animate应该返回可迭代对象包含正在制作动画的艺术家。这就是为什么在杰克的版本中他们回来了line,(实际上是一个元组)而不是line(这是一个单线对象)。可悲的是,文档对此并不清楚!

您可以像这样修复您的版本:

# 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]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

matplotlib imshow():如何制作动画? 的相关文章

随机推荐