With a few simple pattern painted python

1 turtle

turtle This library is really fun, with a few simple lines of code will be able to draw pretty patterns, before the recent bored rolled his own painting ha ha ha ha, share a few of code to draw a similar pattern of colorful lollipops

Import Turtle   
turtle.pensize ( . 5 ) # thickness suggested here provided finer point pen, not too thick Attractive 
Color = [ ' Orange ' , ' Blue ' , ' Yellow ' , ' Black ' , ' Green ' , ' Tomato ' ] # several set your favorite color
 for i in the Range (600 ): # make the next code cycle many times, 600 times is enough, if the front to join initialization, set the large canvas that you can set the number of times more multi- 
    turtle.pencolor (color [i % 6 ]) # set up six color because, if they wish based on other shapes, remember how many how many sides of color, here take the remainder to be adjusted 
    turtle.fd (i * 1.15) # Can go Soso incremental type of function, where a simple set 
    turtle.left ( 62) # 6 I is based on regular deformation painting, each turn left 60 degrees, but in order to cross a sense of slightly increased several times

Well, look at the results

 

2 use matplotlib.pyplot draw a random walk

from Random Import Choice choice # random method can be randomly selected in the results presented in your
 Import matplotlib.pyplot PLT # AS incorporated matplotlib 

class Random_walk ():
     DEF  the __init__ (Self, NUM_POINTS = 5000 ):
         "" " a randomly generated class data stroll "" " 
        self.num_points = NUM_POINTS # initialization, the first x, y values are set to 0, while the need to set the number of trip points (NUM_POINTS) 
        self.x_values = [0] 
        self.y_values = [0 ]
     DEF fill_walk (Self):
         the while len (self.x_values) < self.num_points: # setting cycle times to num_points 
            x_directionChoice = ([. 1, -1 ]) # XY-axis positive and negative directions because every randomly selected forward direction and a distance 
            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)
    def scatter(self):
            plt.scatter(self.x_values,self.y_values,s=5)
            
            plt.show()
            
mx = Random_walk()
mx.fill_walk()
mx.scatter()

2 examples of code is very simple, I lazy, do not like to write notes, direct look at the results of it

 

 

Guess you like

Origin www.cnblogs.com/oslo254804746/p/11864454.html