在Python中为数组着色和设置动画
我正在尝试用Python构建康威的生活游戏的基本版本。我制作了一个0数组作为空白背景,用1替换要着色的点。但是,我无法将颜色指定给0和1并同时设置绘图动画。 以下是完整的代码:在Python中为数组着色和设置动画,python,numpy,matplotlib,Python,Numpy,Matplotlib,我正在尝试用Python构建康威的生活游戏的基本版本。我制作了一个0数组作为空白背景,用1替换要着色的点。但是,我无法将颜色指定给0和1并同时设置绘图动画。 以下是完整的代码: import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation import numpy as np grid = np.zeros((11,11), dtype=np.int16) def neighbors(x,y)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
grid = np.zeros((11,11), dtype=np.int16)
def neighbors(x,y):
n_cells = [grid[x-1, y+1], grid[x, y+1], grid[x+1, y+1],
grid[x-1, y], grid[x+1, y],
grid[x-1, y-1], grid[x, y-1], grid[x+1, y-1]]
return sum(n_cells)
grid[3:6,5]=1
newgrid = grid.copy()
def updatefig(i):
for x in range(10):
for y in range(10):
if grid[x,y]==0:
if neighbors(x,y)==3:
newgrid[x,y]=1
else:
if neighbors(x,y)<2 or neighbors(x,y)>3:
newgrid[x,y]=0
return newgrid
fig, ax = plt.subplots()
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
plt.imshow(grid, cmap='binary')
ani = FuncAnimation(fig, updatefig, interval=100)
plt.show()
导入matplotlib.pyplot作为plt
从matplotlib.animation导入FuncAnimation
将numpy作为np导入
grid=np.zero((11,11),dtype=np.int16)
def邻域(x,y):
n_单元格=[网格[x-1,y+1],网格[x,y+1],网格[x+1,y+1],
网格[x-1,y],网格[x+1,y],
网格[x-1,y-1],网格[x,y-1],网格[x+1,y-1]]
返回和(n个单元格)
网格[3:6,5]=1
newgrid=grid.copy()
def updatefig(一):
对于范围(10)内的x:
对于范围(10)内的y:
如果网格[x,y]==0:
如果邻居(x,y)==3:
新网格[x,y]=1
其他:
如果相邻(x,y)3:
新网格[x,y]=0
返回新网格
图,ax=plt.子批次()
ax=plt.gca()
ax.setxticklabels([])
ax.设置标签([])
ax.set_xticks([])
ax.set_-yticks([])
plt.imshow(网格,cmap='binary')
ani=FuncAnimation(图,updatefig,间隔=100)
plt.show()
如果我在plt.imshow()中使用cmap,我必须给出一个参数,这将导致静态绘图而没有动画。如果我不指定颜色,只运行动画,我会得到一个白色屏幕。我一直在寻找解决方案并尝试,但没有任何运气
如果我能在没有imshow的情况下为0和1分配颜色,我想它会成功的。有什么想法吗
编辑:我意识到我的循环有缺陷,它只提供1个更新的数组。我正试图解决这个问题,但无论如何仍然需要动画。我发现,plt.imshow()和动画都没有问题,问题出在我的循环本身。一旦修好了,它就工作了。 代码如下:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
grid = np.zeros((11,11), dtype=np.int16)
def neighbors(x,y):
#cell = grid[x,y]
n_cells = [grid[x-1, y+1], grid[x, y+1], grid[x+1, y+1],
grid[x-1, y], grid[x+1, y],
grid[x-1, y-1], grid[x, y-1], grid[x+1, y-1]]
return sum(n_cells)
grid[3:6,5]=1
newgrid = grid.copy()
def updatefig(i):
for x in range(10):
for y in range(10):
if grid[x,y]==0:
if neighbors(x,y)==3:
newgrid[x,y]=1
else:
if neighbors(x,y)<2 or neighbors(x,y)>3:
newgrid[x,y]=0
def update_grid():
global grid
grid = newgrid.copy()
update_grid()
plt.imshow(newgrid, cmap='binary')
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xticks([])
ax.set_yticks([])
#plt.imshow(grid, cmap='binary')
ani = FuncAnimation(fig, updatefig, interval=100)
plt.show()
导入matplotlib.pyplot作为plt
从matplotlib.animation导入FuncAnimation
将numpy作为np导入
grid=np.zero((11,11),dtype=np.int16)
def邻域(x,y):
#单元=网格[x,y]
n_单元格=[网格[x-1,y+1],网格[x,y+1],网格[x+1,y+1],
网格[x-1,y],网格[x+1,y],
网格[x-1,y-1],网格[x,y-1],网格[x+1,y-1]]
返回和(n个单元格)
网格[3:6,5]=1
newgrid=grid.copy()
def updatefig(一):
对于范围(10)内的x:
对于范围(10)内的y:
如果网格[x,y]==0:
如果邻居(x,y)==3:
新网格[x,y]=1
其他:
如果相邻(x,y)3:
新网格[x,y]=0
def update_grid():
全球网格
grid=newgrid.copy()
更新网格()
plt.imshow(newgrid,cmap='binary')
ax=plt.gca()
ax.setxticklabels([])
ax.设置标签([])
ax.set_xticks([])
ax.set_-yticks([])
#plt.imshow(网格,cmap='binary')
ani=FuncAnimation(图,updatefig,间隔=100)
plt.show()
我在循环中使用了
imshow
,但即使我取消了动画函数上方的注释,它也能工作。什么是fig
参数?抱歉?我不熟悉这一点,但如果您询问有关fig,ax
语句的问题,我只是在后面的代码中使用它来删除轴标签和记号。您说这是完整的代码,但您有:ani=FuncAnimation(图…
。fig
参数是什么?抱歉,我现在添加了它。我想,无论如何都不会发布代码。