Python實(shí)現(xiàn)的matplotlib動(dòng)畫(huà)演示之細(xì)胞自動(dòng)機(jī)
維基百科上有個(gè)有意思的話題叫細(xì)胞自動(dòng)機(jī):https://en.wikipedia.org/wiki/Cellular_automaton
在20世紀(jì)70年代,一種名為生命游戲的二維細(xì)胞自動(dòng)機(jī)變得廣為人知,特別是在早期的計(jì)算機(jī)界。由約翰 · 康威發(fā)明,馬丁 · 加德納在《科學(xué)美國(guó)人》的一篇文章中推廣,其規(guī)則如下:
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
總結(jié)就是:任何活細(xì)胞在有兩到三個(gè)活鄰居時(shí)能活到下一代,否則死亡。任何有三個(gè)活鄰居的死細(xì)胞會(huì)變成活細(xì)胞,表示繁殖。
在Conway’s Game of Life中,展示了幾種初始狀態(tài):

下面我們用python來(lái)模擬,首先嘗試表示Beacon:
import numpy as np import matplotlib.pyplot as plt universe = np.zeros((6, 6), "byte") # Beacon universe[1:3, 1:3] = 1 universe[3:5, 3:5] = 1 print(universe) im = plt.imshow(universe, cmap="binary")
[[0 0 0 0 0 0] [0 1 1 0 0 0] [0 1 1 0 0 0] [0 0 0 1 1 0] [0 0 0 1 1 0] [0 0 0 0 0 0]]

可以看到已經(jīng)成功的打印出了Beacon的形狀,下面我們繼續(xù)編寫(xiě)細(xì)胞自動(dòng)機(jī)的演化規(guī)則:
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(h):
for x in range(w):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三個(gè)活鄰居的死細(xì)胞都變成了活細(xì)胞,繁殖一樣。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有兩到三個(gè)活鄰居的活細(xì)胞都能活到下一代,否則就會(huì)死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
return universe_new
universe = cellular_auto(universe)
print(universe)
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
[[0 0 0 0 0 0] [0 1 1 0 0 0] [0 1 0 0 0 0] [0 0 0 0 1 0] [0 0 0 1 1 0] [0 0 0 0 0 0]]

ArtistAnimation動(dòng)畫(huà)
基于此我們可以制作matplotlib的動(dòng)畫(huà),下面直接將Blinker、Toad、Beacon都放上去:
from matplotlib import animation
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(h):
for x in range(w):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三個(gè)活鄰居的死細(xì)胞都變成了活細(xì)胞,繁殖一樣。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有兩到三個(gè)活鄰居的活細(xì)胞都能活到下一代,否則就會(huì)死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
return universe_new
universe = np.zeros((12, 12), "byte")
# Blinker
universe[2, 1:4] = 1
# Beacon
universe[4:6, 5:7] = 1
universe[6:8, 7:9] = 1
# Toad
universe[8, 2:5] = 1
universe[9, 1:4] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
frame = []
for _ in range(2):
frame.append((plt.imshow(universe, cmap="binary"),))
universe = cellular_auto(universe)
animation.ArtistAnimation(fig, frame, interval=500, blit=True)
然后我們畫(huà)一下Pulsar:
# Pulsar
universe = np.zeros((17, 17), "byte")
universe[[2, 7, 9, 14], 4:7] = 1
universe[[2, 7, 9, 14], 10:13] = 1
universe[4:7, [2, 7, 9, 14]] = 1
universe[10:13, [2, 7, 9, 14]] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
frame = []
for _ in range(3):
frame.append((plt.imshow(universe, cmap="binary"),))
universe = cellular_auto(universe)
animation.ArtistAnimation(fig, frame, interval=500, blit=True)

FuncAnimation動(dòng)畫(huà)
另一種創(chuàng)建matplotlib動(dòng)畫(huà)的方法是使用FuncAnimation,完整代碼:
from matplotlib import animation
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import HTML
# %matplotlib notebook
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(h):
for x in range(w):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三個(gè)活鄰居的死細(xì)胞都變成了活細(xì)胞,繁殖一樣。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有兩到三個(gè)活鄰居的活細(xì)胞都能活到下一代,否則就會(huì)死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
return universe_new
def update(i=0):
global universe
im.set_data(universe)
universe = cellular_auto(universe)
return im,
# Pulsar
universe = np.zeros((17, 17), "byte")
universe[[2, 7, 9, 14], 4:7] = 1
universe[[2, 7, 9, 14], 10:13] = 1
universe[4:7, [2, 7, 9, 14]] = 1
universe[10:13, [2, 7, 9, 14]] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
plt.show()
anim = animation.FuncAnimation(
fig, update, frames=3, interval=500, blit=True)
HTML(anim.to_jshtml())
這種動(dòng)畫(huà)生成速度較慢,好處是可以導(dǎo)出html文件:
with open("out.html", "w") as f:
f.write(anim.to_jshtml())
還可以保存MP4視頻:
anim.save("out.mp4")
或gif動(dòng)畫(huà):
anim.save("out.gif")
注意:保存MP4視頻或GIF動(dòng)畫(huà),需要事先將ffmpeg配置到環(huán)境變量中
ffmpeg下載地址:
鏈接: https://pan.baidu.com/s/1aioB_BwpKb6LxJs26HbbiQ?pwd=ciui
提取碼: ciui
隨機(jī)生命游戲
接下來(lái),我們創(chuàng)建一個(gè)50*50的二維生命棋盤(pán),并選取其中1500個(gè)位置作為初始活細(xì)胞點(diǎn),我們看看最終生成的動(dòng)畫(huà)如何。
完整代碼如下:
from matplotlib import animation
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
def cellular_auto(universe):
universe_new = universe.copy()
h, w = universe.shape
for y in range(1, h-1):
for x in range(1, w-1):
neighbor_num = universe[x-1:x+2, y-1:y+2].sum()-universe[x, y]
# 任何有三個(gè)活鄰居的死細(xì)胞都變成了活細(xì)胞,繁殖一樣。
if universe[x, y] == 0 and neighbor_num == 3:
universe_new[x, y] = 1
# 任何有兩到三個(gè)活鄰居的活細(xì)胞都能活到下一代,否則就會(huì)死亡。
if universe[x, y] == 1 and neighbor_num not in (2, 3):
universe_new[x, y] = 0
# 邊緣置零
universe[[0, -1]] = 0
universe[:, [0, -1]] = 0
return universe_new
boardsize, pad = 50, 2
universe = np.zeros((boardsize+pad, boardsize+pad), "byte")
# 隨機(jī)選取1500個(gè)點(diǎn)作為初始活細(xì)胞
for i in range(1500):
x, y = np.random.randint(1, boardsize+1, 2)
universe[y, x] = 1
fig = plt.figure()
plt.axis("off")
im = plt.imshow(universe, cmap="binary")
frame = []
for _ in range(200):
frame.append((plt.imshow(universe, cmap="binary"),))
universe = cellular_auto(universe)
animation.ArtistAnimation(fig, frame, interval=50, blit=True)
到此這篇關(guān)于Python實(shí)現(xiàn)的matplotlib動(dòng)畫(huà)演示之細(xì)胞自動(dòng)機(jī)的文章就介紹到這了,更多相關(guān)python matplotlib動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django框架ORM操作數(shù)據(jù)庫(kù)不生效問(wèn)題示例解決方法
本文詳細(xì)描述使用Django 的ORM框架操作PostgreSQL數(shù)據(jù)庫(kù)刪除不生效問(wèn)題的定位過(guò)程及解決方案,并總結(jié)使用ORM框架操作數(shù)據(jù)庫(kù)不生效的問(wèn)題的通用定位方法,感興趣的朋友跟隨小編一起看看吧2023-01-01
python list格式數(shù)據(jù)excel導(dǎo)出方法
今天小編就為大家分享一篇python list格式數(shù)據(jù)excel導(dǎo)出方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
關(guān)于python通過(guò)新建環(huán)境安裝tfx的問(wèn)題
這篇文章主要介紹了python安裝tfx/新建環(huán)境,新建一個(gè)環(huán)境tfx專(zhuān)門(mén)用來(lái)運(yùn)行流水線,這個(gè)環(huán)境安裝python3.8,對(duì)python安裝tfx相關(guān)知識(shí)感興趣的朋友一起看看吧2022-05-05
解決Python報(bào)錯(cuò):ValueError:operands?could?not?be?broadcast?t
這篇文章主要給大家介紹了關(guān)于解決Python報(bào)錯(cuò):ValueError:operands?could?not?be?broadcast?together?with?shapes的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Python獲取excel的數(shù)據(jù)并繪制箱型圖和直方圖的相關(guān)資料,好的圖表能幫助我們深化數(shù)據(jù)的記憶點(diǎn),文中通過(guò)圖文以及代碼示例將實(shí)現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Python通過(guò)Pygame繪制移動(dòng)的矩形實(shí)例代碼
這篇文章主要介紹了Python通過(guò)Pygame繪制移動(dòng)的矩形實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

