I want to show life-game simulation. However this code doesn't show animation but image. Please tell me the solution. I tried "%matplotlib notebook" or "%matplotlib nbagg". The below is my code.
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def f(N,cell):
cell_new=cell.copy()
for i in range(1,cell.shape[0]-1):
for j in range(1,cell.shape[1]-1):
if cell[i,j]==1:
N_shu=cell[i-1:i+2,j-1:j+2].sum()-1
if N_shu==2 or N_shu==3:
cell_new[i,j]=1
else:
cell_new[i,j]=0
else:
N_shu=cell[i-1:i+2,j-1:j+2].sum()
if N_shu==3:
cell_new[i,j]=1
else:
cell_new[i,j]=0
return cell_new
fig=plt.figure(figsize=(15,15))
plt.tick_params(labelbottom=False,labelleft=False,labelright=False,labeltop=False)
ims=[]
N=10
cell=np.zeros((N+2,N+2),dtype=np.int32)
mat=np.random.randint(0,2,size=(N,N),dtype=np.int32)
cell[1:-1,1:-1]=mat
for _ in range(100):
cell=f(N,cell)
im=plt.imshow(cell,cmap=plt.cm.gray_r,animated=True)
ims.append([im])
ani=animation.ArtistAnimation(fig,ims,interval=100)
plt.show()