import matplotlib.pyplot as plt
rw = RandomWalk()
rw.fill_walk()
plt.scatter(rw.x_values, rw.y_values, s =3)
plt.show()
import matplotlib.pyplot as plt
whileTrue:# 创建一个随机漫步实例,并将其包含的各点都绘制出来
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.Reds, edgecolor ='none', s =3)
plt.show()
keep_running =input("Make another walk?(y/n):")if keep_running =='n':break
Make another walk?(y/n):y
Make another walk?(y/n):y
Make another walk?(y/n):n
将绘图函数块放在while循环中,实现模拟多次随机漫步
给点着色,使用颜色映射来指出漫步中各点的先后顺序,并删除各点的黑色轮廓使其更美观
import matplotlib.pyplot as plt
whileTrue:# 创建一个随机漫步实例,并将其包含的各点都绘制出来
rw = RandomWalk()
rw.fill_walk()# 设置窗口的尺寸
plt.figure(dpi =334, figsize =(10,6))
point_numbers =list(range(rw.num_points))
plt.scatter(rw.x_values, rw.y_values, c = point_numbers, cmap = plt.cm.Reds, edgecolor ='none', s =1)# 绘制完成后,重新绘制起点和终点,突出显示
plt.scatter(rw.x_values[0], rw.y_values[0], c ='blue', edgecolor ='none', s =100)
plt.scatter(rw.x_values[-1], rw.y_values[-1], c ='green', edgecolor ='none', s =100)# 隐藏坐标轴
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
E:\Python\Anaconda\lib\site-packages\ipykernel_launcher.py:18: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
E:\Python\Anaconda\lib\site-packages\ipykernel_launcher.py:19: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.