4

我计算了一些结果,它们是 64x64 数组的形式。每个数组都是在另一个时间之后创建的。我想像动画一样一个接一个地显示这些数组。我尝试了很多方法,但没有一个可以工作。我很沮丧,关于动画的 SO 问题无法帮助我让它发挥作用。这不是我第一次尝试这个,但每次我的结果都是一样的:我从来没有让这个工作。

我尝试过的方法:

动态图像

动态图像 2

简单的动画

我拥有的当前代码:

fig, ax = plt.subplots()
def animate(i):
    return imagelist[i] 
def init():
    fig.set_data([],[])
    return fig
ani = animation.FuncAnimation(fig, animate, np.arange(0, 19), init_func=init,
interval=20, blit=True)
plt.show()

这里的 imagelist 是我上面提到的数组的列表(长度为 20、0 到 19)。我的问题是我怎样才能让它工作?

4

3 回答 3

8

几乎完全从您的第一个链接复制(并添加一些评论):

hmpf = ones([4,4])
hmpf[2][1] = 0
imagelist = [ hmpf*i*255./19. for i in range(20) ]

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure() # make figure

# make axesimage object
# the vmin and vmax here are very important to get the color map correct
im = plt.imshow(imagelist[0], cmap=plt.get_cmap('jet'), vmin=0, vmax=255)

# function to update figure
def updatefig(j):
    # set the data in the axesimage object
    im.set_array(imagelist[j])
    # return the artists set
    return [im]
# kick off the animation
ani = animation.FuncAnimation(fig, updatefig, frames=range(20), 
                              interval=50, blit=True)
plt.show()

这动画如我所料

于 2013-09-11T14:51:05.290 回答
3

您是否在 Spyder 的交互式 python 会话中运行?如果是这样,您可能需要运行

    %matplotlib qt

确保动画在自己的窗口中打开,而不是内联显示(它不能内联工作)。

另外,请确保您不会在函数问题中爱上调用 animation.FuncAnimation

于 2015-12-12T17:21:30.070 回答
2

我实现了一个方便的脚本,正好适合您的需要。在这里试试

对于您的示例:

imagelist = YOUR-IMAGE-LIST
def redraw_fn(f, axes):
    img = imagelist[f]
    if not redraw_fn.initialized:
        redraw_fn.im = axes.imshow(img, animated=True)
        redraw_fn.initialized = True
    else:
        redraw_fn.im.set_array(img)
redraw_fn.initialized = False

videofig(len(imagelist), redraw_fn, play_fps=30)
于 2017-05-10T06:36:34.947 回答