Skip to content

A simple Python simulation API that generates some beautiful random walks using the Matplotlib library.

License

Notifications You must be signed in to change notification settings

TheGittyPerson/random-walk

Repository files navigation

RandomWalk

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.

Features

  • 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

How it works

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.walk attributes.

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.

Reference

Instance variables

  • RandomWalk.walk — a Walk object that controls walk behaviour (walk values generation)
  • RandomWalk.graph — a Graph object that controls general Matplotlib.pyplot figure properties
  • RandomWalk.points — a Points object that controls point properties if Randomwalk.graph.type_ = 'scatter'
  • RandomWalk.line — a Line object that controls line properties if Randomwalk.graph.type_ = 'line' (Each of the above has a set() method to set their attributes)
  • RandomWalk.fig and RandomWalk.ax, which are the Matplotlib.pyplot Figure and Axes objects, respectively, used in RandomWalk.build().

Methods

RandomWalk.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.

RandomWalk.show()

Display the Matplotlib.pyplot figure.

RandomWalk.close()

Close the figure window created by RandomWalk.show() and unregister it from pyplot.

Example usage

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!

Project structure

  • random_walk.py — main script; contains the RandomWalk class
  • rw_generator.py — controls movement logic (generates plot coordinates)
  • rw_graph_properties.py — controls Matplotlib graph settings; imported from main script
  • rw_settings.py — controls random walk behaviour settings; imported from main script to be passed to the RWGenerator instance
  • run_me.py — example usage

License

This project is licensed under the MIT License.

About

A simple Python simulation API that generates some beautiful random walks using the Matplotlib library.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages