This is a simple Python simulation API that generates some beautiful random walks using Matplotlib.pyplot.
This project is based on a guided project in Eric Matthes's 2023 book, Python Crash Course (Third Edition), with several modifications, including enhanced customizability.
- Simulates a step-by-step random walk in a Matplotlib graph
- Separates x and y movement logic
- Supports custom distances and directions for each axis
- Allows for a highly customizable graph
- Object-oriented and easy to extend
- Suitable for experiments, simulations, or visualizations
Each step in the walk is generated by rw_generator.py by randomly choosing:
- an x-direction and x-distance
- a y-direction and y-distance
from the lists defined in
RandomWalk.walkattributes.
random_walk.py creates an instance of the generator and plots the values on a graph.
A new set of values is generated each time RandomWalk.build() is called (which calls RWGenerator.fill_walk()).
Graph properties and walk behaviour settings are defined in the instance variables listed in Reference.
RandomWalk.walk— aWalkobject that controls walk behaviour (walk values generation)RandomWalk.graph— aGraphobject that controls general Matplotlib.pyplot figure propertiesRandomWalk.points— aPointsobject that controls point properties ifRandomwalk.graph.type_ = 'scatter'RandomWalk.line— aLineobject that controls line properties ifRandomwalk.graph.type_ = 'line'(Each of the above has aset()method to set their attributes)RandomWalk.figandRandomWalk.ax, which are the Matplotlib.pyplotFigureandAxesobjects, respectively, used inRandomWalk.build().
Create an RWGenerator instance to get random walk values, create a pyplot figure, and build the walk graph.
Returns a tuple with three items; the used Matplotlib Figure object, Axes object, and another tuple containing the x and y values used to generate the walk.
Display the Matplotlib.pyplot figure.
Close the figure window created by RandomWalk.show() and unregister it from pyplot.
from random_walk import RandomWalk
if __name__ == '__main__':
rw = RandomWalk()
rw.walk.set(
steps=80_000,
xdistances=[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
ydistances=[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
)
rw.graph.set(
type_='scatter',
figsize=(10, 6),
dpi=128,
style='seaborn-v0_8',
hide_axes=True,
)
rw.points.set(
alpha=1,
size=1,
colorful=True,
colormap='plasma',
)
rw.line.set(
linewidth=0.5
)
while True:
rw.build()
rw.show()
rw.close()
if input("\nMake another? [y/n]: ").lower() != 'y':
break
See the documentation within rw_graph_properties.py and rw_settings for more customizable attributes.
Note that RandomWalk.line attributes only take effect if RandomWalk.graph.type_ is set to 'line'. This is the same for RandomWalk.points (attributes take effect if set to 'scatter').
Tip: You can use RandomWalk.graph.set(type_='line') to make a visualization of Brownian motion!
random_walk.py— main script; contains theRandomWalkclassrw_generator.py— controls movement logic (generates plot coordinates)rw_graph_properties.py— controls Matplotlib graph settings; imported from main scriptrw_settings.py— controls random walk behaviour settings; imported from main script to be passed to theRWGeneratorinstancerun_me.py— example usage
This project is licensed under the MIT License.