基于matplotlib库下animation、pyplot功能进行的一个生态的模拟程序,参考了一些网上可视化的教程和生态模拟的参数。在本程序中由4种东西构成生态系统,草、草食动物、肉食动物、空地,使用了搜索的算法、random函数来模拟动物进食关系,以及动物的捕食动向,最后通过times间隔每秒展示出animation动态图的画面。
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
def addGrass(_num):
for i in range(_num):
x = np.random.randint(0, size)
y = np.random.randint(0, size)
while grid[x][y] != 0:
x = np.random.randint(0, size)
y = np.random.randint(0, size)
grid[x][y] = 1 # 1代表草
def addGrassEater(_num):
for i in range(_num):
x = np.random.randint(0, size)
y = np.random.randint(0, size)
while grid[x][y] != 0:
x = np.random.randint(0, size)
y = np.random.randint(0, size)
grid[x][y] = 2 # 2代表食草动物
def addMeatEater(_num):
for i in range(_num):
x = np.random.randint(0, size)
y = np.ra