在Python Matplotlib中绘制静态游戏生活模式
有很多Python程序可以用来玩经典的“生命游戏”,其中大多数都提供了漂亮的图形动画,让人能够遵循游戏中出现的奇妙模式。这样的程序可以在计算机上交互使用,但我发现很难找到一个能够为打印页面提供静态图形输出的程序(就像1970年马丁·加德纳(Martin Gardner)的文章中所展示的,该文向全世界展示了生命的游戏:)。有许多程序提供基于文本的生活模式输出,但我还没有发现任何一个程序能够用Matplolib图形做同样的事情。因此,我开始编写一个,如下代码所示:在Python Matplotlib中绘制静态游戏生活模式,python,matplotlib,conways-game-of-life,Python,Matplotlib,Conways Game Of Life,有很多Python程序可以用来玩经典的“生命游戏”,其中大多数都提供了漂亮的图形动画,让人能够遵循游戏中出现的奇妙模式。这样的程序可以在计算机上交互使用,但我发现很难找到一个能够为打印页面提供静态图形输出的程序(就像1970年马丁·加德纳(Martin Gardner)的文章中所展示的,该文向全世界展示了生命的游戏:)。有许多程序提供基于文本的生活模式输出,但我还没有发现任何一个程序能够用Matplolib图形做同样的事情。因此,我开始编写一个,如下代码所示: import matplotlib
import matplotlib.pyplot as plt
def iterate(Z):
shape = len(Z), len(Z[0])
N = [[0,]*(shape[0]+2) for i in range(shape[1]+2)]
# Compute number of neighbours for each cell
for x in range(1,shape[0]-1):
for y in range(1,shape[1]-1):
N[x][y] = Z[x-1][y-1]+Z[x][y-1]+Z[x+1][y-1] \
+ Z[x-1][y] +Z[x+1][y] \
+ Z[x-1][y+1]+Z[x][y+1]+Z[x+1][y+1]
# Update cells
for x in range(1,shape[0]-1):
for y in range(1,shape[1]-1):
if Z[x][y] == 0 and N[x][y] == 3:
Z[x][y] = 1
elif Z[x][y] == 1 and not N[x][y] in [2,3]:
Z[x][y] = 0
return Z
# The 'beehive' pattern
Z = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
n_generations = 10
fig, axes = plt.subplots(2, 5, figsize=(8, 8))
for i in range(n_generations):
iterate(Z)
ax = axes.flat[i]
ax.imshow(Z, interpolation='nearest', cmap=plt.cm.binary)
ax.set_axis_off()
ax.set_title('Generation {}'.format(i+1))
plt.grid(True)
plt.tight_layout()
plt.show()
这是可行的,但远不是好事,因为:
(1) 我希望每个图都显示网格线,这样它们就可以复制加德纳文章中的原始图形,但我还没有找到如何做到这一点
(2) 我还希望能够使用球体而不是正方形来表示活细胞(就像加德纳的文章中出现的那样)
任何对改进此代码的帮助都将不胜感激 对于第一个问题,这是因为您设置了轴。我打开它并做了一些调整:
n_generations = 10
fig, axes = plt.subplots(2, 5, figsize=(16, 8))
for i in range(n_generations):
iterate(Z)
ax = axes.flat[i]
ax.imshow(Z, interpolation='nearest', cmap=plt.cm.binary)
# ax.set_axis_off()
ax.set_xticks(np.arange(10)+.5)
ax.set_yticks(np.arange(10)+.5)
ax.set_xticklabels('')
ax.set_yticklabels('')
ax.set_title('Generation {}'.format(i+1))
plt.tight_layout()
plt.show()
输出:
至于另一个问题。我认为使用
imshow
是不可能的。您可能需要编写一个自定义函数my_plot(Z,ax)
来生成网格线,您需要在相应的位置打勾。使用MultipleLocator
将以1的倍数产生刻度
圆圈是标准的散点标记。可以将数据绘制为散点而不是图像
总之,它可能看起来如下所示,我还使代码更加紧凑
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
def iterate(Z):
# http://www.labri.fr/perso/nrougier/from-python-to-numpy/code/game_of_life_numpy.py
N = (Z[0:-2, 0:-2] + Z[0:-2, 1:-1] + Z[0:-2, 2:] +
Z[1:-1, 0:-2] + Z[1:-1, 2:] +
Z[2: , 0:-2] + Z[2: , 1:-1] + Z[2: , 2:])
birth = (N == 3) & (Z[1:-1, 1:-1] == 0)
survive = ((N == 2) | (N == 3)) & (Z[1:-1, 1:-1] == 1)
Z[...] = 0
Z[1:-1, 1:-1][birth | survive] = 1
return Z
# The 'beehive' pattern
Z = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Z = np.array(Z)
X, Y = np.meshgrid(np.arange(Z.shape[1])+.5, np.arange(Z.shape[0])+.5)
fig, axes = plt.subplots(2, 5, figsize=(8, 4))
for i, ax in enumerate(axes.flat):
Z = iterate(Z)
ax.scatter(X[Z > 0], Y[Z > 0], color="k")
ax.grid(True, color="k")
ax.xaxis.set_major_locator(mticker.MultipleLocator())
ax.yaxis.set_major_locator(mticker.MultipleLocator())
ax.tick_params(size=0, length=0, labelleft=False, labelbottom=False)
ax.set(xlim=(0, Z.shape[1]), ylim=(Z.shape[0], 0),
title='Generation {}'.format(i+1), aspect="equal")
plt.tight_layout()
plt.show()
@ImportanceOfBeingEarnest:我接受你的回答,因为它为将细胞画成球体的问题提供了一个解决方案;情节真的很好。谢谢