使用imshow显示连续的图像/数组作为python中的重复动画

Leo*_*Leo 2 python arrays animation matplotlib

我已经计算了一些结果,它们是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()
Run Code Online (Sandbox Code Playgroud)

这里imagelist是我上面提到的数组列表(长度20,0到19).我的问题是如何让这个工作?

tac*_*ell 5

几乎完全从第一个链接复制(并添加一些注释):

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()
Run Code Online (Sandbox Code Playgroud)

这正如我所期望的那样动画