python应用-随机漫步
2024-09-02 05:15:28
对python应用的一个巩固,以及熟悉matplotlib的用法
效果如下:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 28 22:39:55 2018
@author: pprp
"""
from random import choice
import matplotlib.pyplot as plt
class RandomWalk():
"""a class using to generate random data"""
def __init__(self,num_points=5000):
"""init the class"""
self.num_points=num_points
# start at (0,0)
self.x_val=[0]
self.y_val=[0]
def fill_walk(self):
"""calculate the points"""
while len(self.x_val) < self.num_points:
x_direction=choice([1,-1])
x_distance=choice([0,1,2,3,4,5])
x_step = x_direction * x_distance
y_direction=choice([1,-1])
y_distance=choice([1,2,5,4,0])
y_step = y_direction * y_distance
if x_step == 0 and y_step == 0:
continue
next_x = self.x_val[-1]+x_step
next_y = self.y_val[-1]+y_step
self.x_val.append(next_x)
self.y_val.append(next_y)
rw = RandomWalk(50000)
rw.fill_walk()
plt.tick_params(axis='both',labelsize=14)
point_nums=list(range(rw.num_points))
plt.scatter(rw.x_val,rw.y_val,s=1,c=point_nums,cmap=plt.cm.Blues,edgecolors='none')
# plot the start point and end point
plt.scatter(0,0,c='green',edgecolors='none',s=100)
plt.scatter(rw.x_val[-1],rw.y_val[-1],c='red',edgecolors='none',s=100)
# set figure width and height
plt.figure(dpi=1280,figsize=(10,6))
plt.show()
最新文章
- 统计代码git提交的行数
- Otsu algorithm
- [已解决] java.net.InetAddress.getHostName() 阻塞问题
- springmore-让编程更容易
- 高效PHP开发注意事项
- Android 进度条
- ImageOptim 图片压缩工具
- php访问类静态属性
- NOIP2014酱油记
- 原创+部分引用啦:C# Winform界面中的分隔线问题
- Android 新兴的UI模式——侧边导航栏【转】
- jmeter命令行运行-单节点
- 基于react全家桶+antd-design+webpack2+node+express+mongodb开发的前后台博客系统
- vue.js购物车
- Nodejs之路:非I/O的异步API
- loadrunner&#160;脚本优化-关联设置
- aop 记录用户操作(一)
- 在 Laravel 5.1 中使用 Pjax
- ReactJS表单handleChange
- sed 以及 awk用法
热门文章
- my sql 两个 索引 时的 union 与 or 的比较
- js的字符串charAt()方法
- EUI组件之HScrollBar VScrollBar (动态设置滑块图片)
- 170622、springboot编程之JPA操作数据库
- Asp.net读取和写入txt文件方法(实例)!
- Process Monitor分析某个应用行为
- talib 中文文档(十一):Cycle Indicator Functions 周期指标
- 005-spring cache-配置缓存存储
- HDFS的工作流程分析
- /etc/rc.d/rc.local linux启动自动开启某些服务(转)