基于matplotlib库下animation、pyplot功能进行的一个生态的模拟程序,参考了一些网上可视化的教程和生态模拟的参数。在本程序中由4种东西构成生态系统,草、草食动物、肉食动物、空地,使用了搜索的算法、random函数来模拟动物进食关系,以及动物的捕食动向,最后通过times间隔每秒展示出animation动态图的画面。

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as npdef 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.random.randint(0, size)while grid[x][y] != 0 or growAround(x, y, 2) == [-1, -1]:x = np.random.randint(0, size)y = np.random.randint(0, size)grid[x][y] = 3  # 3代表食肉动物def growAround(_x, _y, _id):field = []if _x-1 < 0:x_begin = 0else:x_begin = _x-1if _y-1 < 0:y_begin = 0else:y_begin = _y-1if _x+1 > size-1:x_end = size-1else:x_end = _x+1if _y+1 > size-1:y_end = size-1else:y_end = _y+1for i in range(x_begin, x_end+1):for j in range(y_begin, y_end+1):if grid[i][j] == _id or grid[i][j] == _id*10:  # 2代表食草动物,1代表草,0代表空地field += [[i, j]]if len(field) == 0:  # 没有食物或者空地return [-1, -1]else:count = np.random.randint(0, len(field))return field[count]def fieldUpdate():for i in range(size):for j in range(size):if grid[i][j] == 30:grid[i][j] = 3elif grid[i][j] == 20:grid[i][j] = 2elif grid[i][j] == 10:grid[i][j] = 1def data_gen():for count in range(times):timesText.set_text('times: %d' % (count+1))for i in range(size):for j in range(size):if grid[i][j] == 3:place = growAround(i, j, 2)if place == [-1, -1]:grid[i][j] = 0  # 食肉动物死亡else:grid[i][j] = 0grid[place[0]][place[1]] = 30  # 食肉动物进食并移动growth = growAround(i, j, 0)if growth != [-1, -1]:grid[growth[0]][growth[1]] = 30  # 食肉动物繁殖if grid[i][j] == 2:place = growAround(i, j, 1)if place == [-1, -1]:grid[i][j] = 0  # 食草动物死亡else:grid[i][j] = 0grid[place[0]][place[1]] = 20  # 食草动物进食并移动growth = growAround(i, j, 0)if growth != [-1, -1]:grid[growth[0]][growth[1]] = 20  # 食草动物繁殖elif grid[i][j] == 1:growth = growAround(i, j, 0)if growth != [-1, -1]:grid[growth[0]][growth[1]] = 10  # 草生长fieldUpdate()yield griddef update(_data):ax.imshow(_data, interpolation='nearest', cmap='Set3', norm=norm)return axtimes = 100  # 迭代次数
size = 40
grid = np.zeros((size, size))  # 0代表空地
addGrass(1200)
addGrassEater(150)
addMeatEater(30)fig = plt.figure()
ax = plt.subplot(111)
norm = matplotlib.colors.Normalize(vmin=0, vmax=3)  # 固定数值对应的颜色映射
gci = ax.imshow(grid, interpolation='nearest', cmap='Set3', norm=norm)
ax.set_xticks([])
ax.set_yticks([])
cbar = plt.colorbar(gci)
cbar.set_ticks(np.linspace(0, 3, 4))
cbar.set_ticklabels(('Space', 'Grass', 'GrassEater', 'MeatEater'))
timesText = plt.text(-2, -2, 'times: 0')
ani = animation.FuncAnimation(fig, update, data_gen, interval=1000, repeat=False)plt.show()

python模拟生态系统相关推荐

  1. python模拟按键_python 模拟按键放在模拟器Python初学者的17个技巧

    Python初学者的17个技巧,有需要的朋友可以参考下. W WW.002pc .COM认为此文章对<python 模拟按键放在模拟器Python初学者的17个技巧>说的很在理. 交换变量 ...

  2. Python模拟赌博实验,赌博为什么能赌到倾家荡产?

    Python模拟赌博实验 作者:dalalaa http://www.jianshu.com/p/2d919a028600 概述 前言 实验思路 实验步骤 统计数据 结论 阿广说 福利一刻 推荐阅读 ...

  3. python 模拟键盘_Python 模拟键盘输入 | 学步园

    Python 模拟键盘输入,留着备用. #coding=utf-8 import win32gui,win32api,win32con import time import threading def ...

  4. python 模拟HTTP服务端

    使用python模拟http服务端,自定义返回信息头部:#!/usr/bin/python import os from BaseHTTPServer import BaseHTTPRequestHa ...

  5. python模拟登录qq账号密码_最新的Python模拟登陆QQ脚本,一键批量登录,强行过验证!...

    Python模拟QQ批量登陆脚本,以下代码附带解释以便于理解思路. Python代码: #coding=utf-8 import os import time import win32gui impo ...

  6. 利用Python模拟鼠标自动完成MM32-LINK程序下载

    简 介: 编写了利用Python控制MM32-LINK自动下载程序,这可以减少在开发过程中的操作. MM32-LINK在打开程序过程中,对话框的标题出现错误,"Load form file& ...

  7. python 通过title判断_利用Python模拟GitHub登录

    点击关注,我们共同每天进步一点点! 最近学习了Fiddler抓包工具的简单使用,通过抓包,我们可以抓取到HTTP请求,并对其进行分析.现在我准备尝试着结合Python来模拟GitHub登录. Fidd ...

  8. Python模拟弹道轨迹

    http://www.itongji.cn/cms/article/articledetails?articleid=5029 最近美国把萨德系统部署到韩国,一时心血来潮就用python模拟最简单的弹 ...

  9. python post请求 上传图片_利用python模拟实现POST请求提交图片的方法

    本文主要给大家介绍的是关于利用python模拟实现POST请求提交图片的方法,分享出来供大家参考学习,下面来一看看详细的介绍: 使用requests来模拟HTTP请求本来是一件非常轻松的事情,比如上传 ...

最新文章

  1. 【Spring】Spring系列6之Spring整合Hibernate
  2. [Java拾遗三]JavaWeb基础之Servlet
  3. HDU 1556 Color the Ball 线段树 题解
  4. php可以控制硬件吗,控制面板的作用是设置硬件接口吗?
  5. nmd测试软件中文,阿迪达斯Boost传说中的踩屎感,NMD跑步真假对比测评!
  6. Mysql整库导出导入
  7. 数据结构 5-0 树与二叉树总结
  8. Java多线程(五)——多线程的多线程池
  9. 我的世界java版刷铁机_我的世界:教你建造新版本中最简单的刷铁机,效率高人人学的会...
  10. PHP+Redis实现高并发
  11. 洞态IAST源码分析及吐槽
  12. 【数据分析案例】留存率分析方法
  13. qchart 怎么点击一下 出一条线_陈奕迅唱歌技巧,学好这几点,你也能唱出更具有穿透力和磁性的声音...
  14. 电脑卡顿反应慢怎么办?这几招教给你!
  15. ios 区分iphone ipod ipad的方法及获取设备名称。
  16. 分享几个好用的导航导航网站
  17. 高效进行接口测试,简单易懂
  18. 谷歌基情录:TensorFlow、Hadoop、MapReduce 都靠他们诞生!
  19. 自我反思--table的简单数据分页
  20. 您玩过这十三款iOS策略游戏了吗

热门文章

  1. SCCB协议 verilog状态机定时器
  2. termios的例子
  3. 基于php微信小程序购物商城 校园二手商品 图书鲜花商城 毕业设计(3)微信用户登录
  4. 快速去除电脑弹窗广告
  5. client Sdk
  6. Jscript函数的使用
  7. 火狐浏览器更新版本之后总是在当前页面打开新链接覆盖掉原先内容
  8. http协议的状态码 200、301、304、404、502 HTTP状态码解释
  9. 如何使用cleanmymac文件粉碎机
  10. 二叉树、二叉搜索树,平衡二叉树(旋转)红黑树(红黑规则)