In [1]:
    
from matplotlib import pyplot as plt
import numpy as np
from matplotlib import animation
    
In [2]:
    
n_steps = 10
size = 20
data = np.random.random((n_steps, size, size))
data = np.array(data.round(), dtype=int)
    
In [3]:
    
fig, ax = plt.subplots()
img = ax.matshow(data[0,:,:], cmap="binary")
    
In [4]:
    
def update_img(step):
    img.set_data(data[step,:,:])
    return img
    
In [5]:
    
anim = animation.FuncAnimation(fig, update_img, frames=n_steps)
anim.save("animation.mp4")
    
In [6]:
    
plt.show()