SoFunction
Updated on 2024-11-16

Example of Drawing a Walking Map with Python Explained

Let's first look at the code:

import  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)
rw=RandomWalk()
rw.fill_walk()
(rw.x_values,rw.y_values,s=1)
()

The plotted diagram is shown below:

This code plots 5,000 data points that are distributed completely randomly. Each time you run the code it goes in a different direction.

Instance Extension

from random import choice
  
class RandomWalk():
 """A Class for Generating Random Walk Data."""
  
 def __init__(self,num_points=5000):
 """Initializing Random Walk Properties."""
 self.num_points = num_points
  
 # All random walks start at (0,0) #
 self.x_values = [0]
 self.y_values = [0]
  
 def fill_walk(self):
 """Compute all points contained in a random walk."""
  
 # Keep walking until the list reaches the specified length
 while len(self.x_values) < self.num_points:
  # Determine the direction of travel and the distance to be traveled in that direction
  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 * x_distance
  
  # Refuse to stand still
  if x_step == 0 and y_step == 0:
  continue
  
  # Calculate the x and y values for the next point
  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)
import  as plt 
  
from random_walk import RandomWalk
  
# Create a RandomWalk instance and plot all the points it contains
rw = RandomWalk(50000)
rw.fill_walk()
  
# Set the size of the drawing window
(dpi=80,figsize=(10,6))
  
# Set points increase color depth in sequential order
point_numbers = list(range(rw.num_points))
(rw.x_values,rw.y_values,c=point_numbers,cmap=,
 edgecolor='none',s=1)
  
# Highlight the start and end points, with the start point set in green and the end point set in red
(0,0,c='green',edgecolor='none',s=100)
(rw.x_values[-1],rw.y_values[-1],c='red',edgecolor='none',s=100)
  
# Hide the axes
().get_xaxis().set_visible(False)
().get_yaxis().set_visible(False)
  
()

The second example is more or less the same, it is using PY3.5, you can test it locally.

To this point, this article on the use of Python to draw a walking map example explains the article is introduced to this, more related to the use of Python to draw the content of the walking map, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!