首页 / PYTHON / python应用-随机漫步
python应用-随机漫步
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python应用-随机漫步,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1232字,纯文字阅读大概需要2分钟。
内容图文
![python应用-随机漫步](/upload/InfoBanner/zyjiaocheng/856/ac93b72773804a769427e8997920ee26.jpg)
对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()
内容总结
以上是互联网集市为您收集整理的python应用-随机漫步全部内容,希望文章能够帮你解决python应用-随机漫步所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。