ライフゲーム / Python / 時々刻々変化する2次元排列をアニメ化する

matplotlib.animation.FuncAnimation()を使ってみる。下の例はライフゲームではない。

#%matplotlib nbagg
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# この函数を一定時間ごとに繰り返し実行することにする。
# 引数iはFuncAnimation()から渡されるフレーム番号(を受けるための変数)であるがここでは使っていない。
def update(i):
    plt.clf()
    mat = np.random.randint(0, 2, [20, 30]) # 20行30列の2値乱数排列を生成して、
    plt.imshow(mat, cmap="binary") # 図として表示する。

# 表示領域を設定しておく。
fig = plt.figure()

# 「どの表示領域(fig)」で「どの函数(update)」を「何ミリ秒おき(interval=)」に実行するのかを指定する。
anim = FuncAnimation(fig, update, interval=500)
plt.show()

実行結果(ライフゲームではない):
youtu.be