用Python绘制漫步图实例讲解


Posted in Python onFebruary 26, 2020

我们首先来看下代码:

import matplotlib.pyplot as plt
from random import choice
class RandomWalk():
 def __init__(self,num_points=5000):
  self.num_points=num_points
  self.x_values=[0]
  self.y_values=[0]
 def fill_walk(self):
  while len(self.x_values)<self.num_points:
   x_direction=choice([1,-1])
   x_distance=choice([0,1,2,3,4])
   x_step=x_direction*x_distance
   y_direction=choice([1,-1])
   y_distance=choice([0,1,2,3,4])
   y_step=y_direction*y_distance
   if x_step==0 and y_step==0:
    continue
   next_x=self.x_values[-1]+x_step
   next_y=self.y_values[-1]+y_step
   self.x_values.append(next_x)
   self.y_values.append(next_y)
rw=RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values,rw.y_values,s=1)
plt.show()

绘制出的图如下所示:

用Python绘制漫步图实例讲解

这段代码绘制了5000个数据点,这些点的分布完全是随机的。每次运行代码都会有不同的走向。

实例扩展

from random import choice
  
class RandomWalk():
 """一个生成随机漫步数据的类"""
  
 def __init__(self,num_points=5000):
 """初始化随机漫步的属性"""
 self.num_points = num_points
  
 #所有随机漫步都始于(0,0)
 self.x_values = [0]
 self.y_values = [0]
  
 def fill_walk(self):
 """计算随机漫步包含的所有点"""
  
 #不断漫步,直到列表达到指定的长度
 while len(self.x_values) < self.num_points:
  # 决定前进方向以及沿这个方向前进的距离
  x_direction = choice([1,-1])
  x_distance = choice([0,1,2,3,4])
  x_step = x_direction * x_distance
  
  y_direction = choice([1,-1])
  y_distance = choice([0,1,2,3,4])
  y_step = y_direction * x_distance
  
  # 拒绝原地踏步
  if x_step == 0 and y_step == 0:
  continue
  
  #计算下一个点的x和y值
  next_x = self.x_values[-1] + x_step
  next_y = self.y_values[-1] + y_step
  
  self.x_values.append(next_x)
  self.y_values.append(next_y)
import matplotlib.pyplot as plt 
  
from random_walk import RandomWalk
  
# 创建一个RandomWalk实例,并将其包含的点都绘制出来
rw = RandomWalk(50000)
rw.fill_walk()
  
# 设置绘图窗口的尺寸
plt.figure(dpi=80,figsize=(10,6))
  
# 设置点按先后顺序增加颜色深度
point_numbers = list(range(rw.num_points))
plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,
 edgecolor='none',s=1)
  
# 突出起点和终点,起点设置为绿色,终点设置为红色
plt.scatter(0,0,c='green',edgecolor='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none',s=100)
  
# 隐藏坐标轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
  
plt.show()

第二个实例内容差不多,是用的PY3.5,大家可以本地测试下。

到此这篇关于用Python绘制漫步图实例讲解的文章就介绍到这了,更多相关使用Python绘制漫步图内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 文件操作实现代码
Oct 07 Python
python中迭代器(iterator)用法实例分析
Apr 29 Python
Windows环境下python环境安装使用图文教程
Mar 13 Python
python实现汉诺塔算法
Mar 01 Python
django框架之cookie/session的使用示例(小结)
Oct 15 Python
Python面向对象之类和对象实例详解
Dec 10 Python
解决PyCharm控制台输出乱码的问题
Jan 16 Python
python爬虫实现中英翻译词典
Jun 25 Python
基于Python的ModbusTCP客户端实现详解
Jul 13 Python
flask框架蓝图和子域名配置详解
Jan 25 Python
python lambda函数及三个常用的高阶函数
Feb 05 Python
浅谈优化Django ORM中的性能问题
Jul 09 Python
Django单元测试中Fixtures的使用方法
Feb 26 #Python
python 解压、复制、删除 文件的实例代码
Feb 26 #Python
Python递归调用实现数字累加的代码
Feb 25 #Python
python烟花效果的代码实例
Feb 25 #Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
Feb 25 #Python
使用python绘制cdf的多种实现方法
Feb 25 #Python
python GUI库图形界面开发之PyQt5开发环境配置与基础使用
Feb 25 #Python
You might like
session 的生命周期是多长
2006/10/09 PHP
如何在PHP中使用正则表达式进行查找替换
2013/06/13 PHP
PHP 基于Yii框架中使用smarty模板的方法详解
2013/06/13 PHP
CI框架学习笔记(一) - 环境安装、基本术语和框架流程
2014/10/26 PHP
php采用curl模仿登录人人网发布动态的方法
2014/11/07 PHP
简单的php+mysql聊天室实现方法(附源码)
2016/01/05 PHP
按钮JS复制文本框和表格的代码
2011/04/01 Javascript
ajax 同步请求和异步请求的差异分析
2011/07/04 Javascript
jquery实现全选、反选、获得所有选中的checkbox
2020/09/13 Javascript
javascript检测flash插件是否被禁用的方法
2016/01/14 Javascript
JS+CSS3实现超炫的散列画廊特效
2016/07/16 Javascript
javascript鼠标跟随运动3种效果(眼球效果,苹果菜单,方向跟随)
2016/10/27 Javascript
基于jQuery实现Accordion手风琴自定义插件
2020/10/13 Javascript
js判断节假日实例代码
2017/12/27 Javascript
NodeJS读取分析Nginx错误日志的方法
2019/05/14 NodeJs
js+css实现扇形导航效果
2020/08/18 Javascript
[02:51]DOTA2战队出征照拍摄花絮 TI3明星化身时尚男模
2013/07/22 DOTA
Django的URLconf中使用缺省视图参数的方法
2015/07/18 Python
python 构造三维全零数组的方法
2018/11/12 Python
Python 获取 datax 执行结果保存到数据库的方法
2019/07/11 Python
详解Python3 pickle模块用法
2019/09/16 Python
Python3批量创建Crowd用户并分配组
2020/05/20 Python
python 解决函数返回return的问题
2020/12/05 Python
美国从事品牌鞋类零售的连锁店:Famous Footwear
2016/08/25 全球购物
全球度假村:Club Med
2017/11/27 全球购物
DogBuddy荷兰:找到你最完美的狗保姆
2019/04/17 全球购物
Groupon法国官方网站:特卖和网上购物高达-70%
2019/09/02 全球购物
热能动力工程毕业生自荐信
2013/11/07 职场文书
八年级美术教学反思
2014/02/02 职场文书
北京申奥口号
2014/06/19 职场文书
三问三解心得体会
2014/09/05 职场文书
2015年读书月活动总结
2015/03/26 职场文书
婚庆主持词大全
2015/06/30 职场文书
高中生物教学反思
2016/02/20 职场文书
一文搞懂如何实现Go 超时控制
2021/03/30 Python
死磕 java同步系列之synchronized解析
2021/06/28 Java/Android