且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

matplotlib imshow():如何动画?

更新时间:2023-10-24 23:27:28

您是非常密切的,但是有一个错误 - 的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]