用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随机生成数模块random使用实例
Apr 13 Python
python实现批量下载新浪博客的方法
Jun 15 Python
shelve  用来持久化任意的Python对象实例代码
Oct 12 Python
Python使用正则表达式抓取网页图片的方法示例
Apr 21 Python
python构建自定义回调函数详解
Jun 20 Python
pandas多级分组实现排序的方法
Apr 20 Python
Python 利用高德地图api实现经纬度与地址的批量转换
Aug 14 Python
Pytorch Tensor 输出为txt和mat格式方式
Jan 03 Python
如何利用Python动态模拟太阳系运转
Sep 04 Python
python爬虫筛选工作实例讲解
Nov 23 Python
利用Python实现最小二乘法与梯度下降算法
Feb 21 Python
Python Pandas常用函数方法总结
Jun 15 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
一个程序下载的管理程序(三)
2006/10/09 PHP
php的正则处理函数总结分析
2008/06/20 PHP
php for 循环语句使用方法详细说明
2010/05/09 PHP
shopex主机报错误请求解决方案(No such file or directory)
2011/12/27 PHP
php实现微信公众号无限群发
2015/10/11 PHP
PHP简单获取及判断提交来源的方法
2016/04/22 PHP
Yii框架引入coreseek分页功能示例
2019/02/08 PHP
使用laravel和ECharts实现折线图效果的例子
2019/10/09 PHP
简约JS日历控件 实例代码
2013/07/12 Javascript
jQuery监控文本框事件并作相应处理的方法
2015/04/16 Javascript
Jquery中使用show()与hide()方法动画显示和隐藏图片
2015/10/08 Javascript
Vue通过input筛选数据
2020/10/26 Javascript
JavaScript实现短信倒计时60s
2017/10/09 Javascript
微信小程序module.exports模块化操作实例浅析
2018/12/20 Javascript
JS Web Flex弹性盒子模型代码实例
2020/03/10 Javascript
Vue项目页面跳转时浏览器窗口上方显示进度条功能
2020/03/26 Javascript
利用Python爬取可用的代理IP
2016/08/18 Python
Python中模块string.py详解
2017/03/12 Python
python 拷贝特定后缀名文件,并保留原始目录结构的实例
2018/04/27 Python
Python subprocess模块常见用法分析
2018/06/12 Python
Python XML转Json之XML2Dict的使用方法
2019/01/15 Python
Python 20行简单实现有道在线翻译的详解
2019/05/15 Python
Django中多种重定向方法使用详解
2019/07/17 Python
Python Numpy 自然数填充数组的实现
2019/11/28 Python
python中if及if-else如何使用
2020/06/02 Python
浅析Python 抽象工厂模式的优缺点
2020/07/13 Python
法国高保真音响和家庭影院商店:Son Video
2019/04/26 全球购物
酒店公关部经理岗位职责
2013/11/24 职场文书
产品开发计划书
2014/04/27 职场文书
关于环保的活动方案
2014/08/25 职场文书
优秀教师先进个人事迹材料
2014/08/31 职场文书
期中考试复习计划
2015/01/19 职场文书
发言稿之优秀教师篇
2019/09/26 职场文书
Pytorch DataLoader shuffle验证方式
2021/06/02 Python
「睡美人」爱洛公主粘土人开订
2022/03/22 日漫
Spring依赖注入多种类型数据的示例代码
2022/03/31 Java/Android