Data Visualization python (a) - FIG draw random walk

Data Visualization refers to explore through visual representation of data, it is closely related to data mining.

python has a range of visualization and analysis tools, is one of the most popular tools matplotlib, it is drawing a math library.

Plotting random walk FIG.

  Acquiring random library using a random number, plotted with matplotlib

1. Create a class, for each point x to generate a random walk through the two reservoirs, y coordinates

code show as below:

from Random Import Choice
 class RandomWalk ():
     DEF  the __init__ (Self, NumPoints = 5000 ): 
        self.numpoints = NumPoints     # predetermined number of walk, the default value 5000 
        self.x_values = [0]    # X-axis 
        self.y_values = [0]    # Y-axis 

    DEF get_step (Self): 
        direction = choice ([. 1, -1])         # randomly selected direction 
        Distance = choice ([0,1,2,3,4])     # Walking length 
        STEP direction * = Distance
         return STEP 

    DEF fill_walk (Self):  # Generates two lists of length numpoint
         the while len (self.x_values) < self.numpoints: 
            x_step = self.get_step () 
            y_step = self.get_step () 

            IF x_step == 0 and y_step == 0:      # prevent situ Fixed 
                Continue 

            x_next = self.x_values [-1] + x_step      # point displacement x axis 
            y_next self.y_values = [-1] + y_step      # displacement in the y-axis 

            self.x_values.append (x_next)   # the x-axis value of a point 
            self.y_values.append (y_next)   # Y-axis value of a point

Plotting the following function

Import matplotlib.pyplot AS PLT
 from RandomWalk Import RandomWalk 

the while True: 
    RW = RandomWalk (10000)     # Set times to walk 10000 
    rw.fill_walk ()       # generates two stored x, y-axis values list 
    plt.figure (figsize = ( 15,8))   # set window size 
    plt.scatter (rw.x_values, rw.y_values, rw.y_values = C, S =. 1)   # plotted scattergram 
    # plt.plot (rw.x_values, rw.y_values, as linewidth = 3) # line graph 
    plt.show () 

    In Flag = INPUT ( " whether to continue drawing (Y / n-): " )
     iF In Flag == ' n- ' :
        break

 

operation result

 

 

 

 

Guess you like

Origin www.cnblogs.com/hyz1900457346/p/11962826.html