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) if __name__ == "__main__":
#只要程序处于活动状态,就不断地模拟随机漫步
while True:
rw = Randomwalk()
rw.fill_walk()
plt.scatter(rw.x_values,rw.y_values,s=15)
plt.show() keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break

呈现的图像:

还处于活动状态:

按 y 则会继续生成:

给点着色:

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) if __name__ == "__main__":
while True:
rw = Randomwalk()
rw.fill_walk()
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=15)
plt.show() keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break

图像呈现:

图像可以看到是一个渐变的过程

重新绘制起点和终点

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) if __name__ == "__main__":
while True:
rw = Randomwalk()
rw.fill_walk()
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=15) plt.scatter(0,0,c='green',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none')
plt.show() keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break

图像呈现:

隐藏我们的坐标轴:

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) if __name__ == "__main__":
while True:
rw = Randomwalk()
rw.fill_walk()
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=15) plt.scatter(0,0,c='green',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none') plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show() keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break

图像呈现:

增加点数:

import matplotlib.pyplot as plt
from random import choice class Randomwalk():
def __init__(self,num_points=50000):
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) if __name__ == "__main__":
while True:
rw = Randomwalk()
rw.fill_walk()
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',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none') plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show() keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break

图像呈现:

犹如一朵云

调整尺寸大小:

import matplotlib.pyplot as plt
from random import choice class Randomwalk():
def __init__(self,num_points=50000):
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) if __name__ == "__main__":
while True:
rw = Randomwalk()
rw.fill_walk()
plt.figure(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',edgecolors='none',s=100)
plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none') plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
plt.show() keep_running = input("Make another walk? (y/n): ")
if keep_running == 'n':
break

图像呈现:

最新文章

  1. LinuxStudyNote
  2. Curl参数一览
  3. 海明距离hamming distance
  4. JS轮播图
  5. 在cmd命令行下登录本地oracle数据库与服务器上的oracle
  6. Linux下l2tp客户端xl2tpd的安装配置
  7. Android传感器概述(六)
  8. 【jQuery】jQuery API 过 一 遍
  9. smarty 中时间格式化的用法
  10. 主要讲下hack的兼容用法,比较浅,哈哈
  11. Django之跨域请求
  12. 执行grunt命令报错 Cannot find module &#39;coffee-script&#39;
  13. python(62):保留两位小数
  14. 搭建Harbor企业级docker仓库
  15. 2.18 C++类与static关键字
  16. Oracle 扩展表空间大小的几种方式
  17. Labview 查看一次while循环运行的时间
  18. MyBatis 一级缓存避坑
  19. Milk Pails(BFS)
  20. Express中间件简单的实现原理

热门文章

  1. Selenium 2自动化测试实战33(带unittest的脚本分析)
  2. Attention机制在深度学习推荐算法中的应用(转载)
  3. React Native项目实战
  4. 文件夹的层级选择&lt; OC实现 &gt;
  5. jenkins常用插件安装
  6. DISCUZ论坛各大功能模块入口文件介绍
  7. [转帖]Dockerfile 中 ENTRYPOINT 与 CMD 的区别
  8. Linux-echo:打印彩色输出
  9. python入门小结
  10. spring-boot 使用servlet2.5(四)