zoukankan      html  css  js  c++  java
  • 使用plot绘制随机漫步点位图

    from random import choice
    import matplotlib.pyplot as plt
    class RandomWalk():
        """随机漫步的类,三个参数:1,移动的步数;2,x的坐标点;3,y的坐标"""
        def __init__(self,floots=5000):
            self.floots=floots
            self.x_values=[0]
            self.y_values=[0]
    
        def fill_walk(self):
            """计算随机漫步所有的点"""
            while  len(self.x_values)<self.floots:
                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)
    
    while True:
        rw =RandomWalk(5000)
        rw.fill_walk()
        #设置绘制窗口尺寸
        plt.figure(dpi=128,figsize=(10,6))
    
        plt.scatter(rw.x_values,rw.y_values,s=15,c=(0.9,0.2,0.6))
        plt.scatter(0,0,c=(0,0,1),s=100)    #标记起点
        plt.scatter(rw.x_values[-1],rw.y_values[-1],c=(1,0,0),s=100)    #标记终点
        #隐藏坐标轴
        plt.axes().get_xaxis().set_visible(False)
        plt.axes().get_yaxis().set_visible(False)
        plt.show()
    
        keep_runing=input("(Y/N):")
        if keep_runing.lower()=='n':
            break
  • 相关阅读:
    AGC015E Mr.Aoki Incubator
    luogu P3520 [POI2011]SMI-Garbage
    442.Find All Duplicates in an Array
    SICP_2.61-2.62
    sicp_2.59-2.60
    SICP_2.58
    SICP_2.56-2.57
    SICP_2.53-2.55
    SICP_2.52-2.53
    SICP_2.50-2.51
  • 原文地址:https://www.cnblogs.com/ghgxm520/p/8580327.html
Copyright © 2011-2022 走看看