Animated 3D Random Walk in Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to create an animated 3D random walk plot using Matplotlib library in Python. We will create a 3D plot and simulate a random walk with 40 particles that move randomly in 3D space.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/animation_creation("`Animation Creation`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} matplotlib/figures_axes -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} matplotlib/line_plots -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} matplotlib/animation_creation -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/variables_data_types -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/for_loops -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/list_comprehensions -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/lists -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/tuples -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/function_definition -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/default_arguments -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/importing_modules -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/standard_libraries -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/math_random -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/data_collections -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/numerical_computing -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/data_visualization -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} python/build_in_functions -.-> lab-48901{{"`Animated 3D Random Walk in Matplotlib`"}} end

Import Required Libraries

We begin by importing the required libraries. We will use numpy to generate random numbers and matplotlib to create the plot.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

Define Random Walk Function

We define a function that generates a random walk with a given number of steps and maximum step size. The function takes two inputs: num_steps is the total number of steps in the random walk and max_step is the maximum size of each step. We use numpy.random to generate random numbers for the steps and numpy.cumsum to compute the cumulative sum of the steps to obtain the final position.

def random_walk(num_steps, max_step=0.05):
    """Return a 3D random walk as (num_steps, 3) array."""
    start_pos = np.random.random(3)
    steps = np.random.uniform(-max_step, max_step, size=(num_steps, 3))
    walk = start_pos + np.cumsum(steps, axis=0)
    return walk

Define Update Function

We define a function that updates the plot for each frame of the animation. The function takes three inputs: num is the current frame number, walks is a list of all the random walks, and lines is a list of all the lines in the plot. For each line and walk, we update the data for the x, y, and z coordinates of the line up to the current frame number. We use line.set_data() and line.set_3d_properties() to update the x-y and z coordinates, respectively.

def update_lines(num, walks, lines):
    for line, walk in zip(lines, walks):
        ## NOTE: there is no .set_data() for 3 dim data...
        line.set_data(walk[:num, :2].T)
        line.set_3d_properties(walk[:num, 2])
    return lines

Generate Random Walks

We generate 40 random walks with 30 steps each using the random_walk() function defined earlier. We store all the random walks in a list called walks.

## Data: 40 random walks as (num_steps, 3) arrays
num_steps = 30
walks = [random_walk(num_steps) for index in range(40)]

Create 3D Plot

We create a 3D plot using matplotlib. We add an empty line for each random walk to the plot. We set the limits for the x, y, and z axes to be between 0 and 1.

## Attaching 3D axis to the figure
fig = plt.figure()
ax = fig.add_subplot(projection="3d")

## Create lines initially without data
lines = [ax.plot([], [], [])[0] for _ in walks]

## Setting the axes properties
ax.set(xlim3d=(0, 1), xlabel='X')
ax.set(ylim3d=(0, 1), ylabel='Y')
ax.set(zlim3d=(0, 1), zlabel='Z')

Create Animation

We create an animation using the FuncAnimation class from matplotlib.animation. We pass the figure object, the update function, the total number of frames (which is equal to the number of steps in the random walks), the list of all random walks, and the list of all lines as arguments to the FuncAnimation constructor.

## Creating the Animation object
ani = animation.FuncAnimation(
    fig, update_lines, num_steps, fargs=(walks, lines), interval=100)

Display Animation

Finally, we display the animation using plt.show().

plt.show()

Summary

We have learned how to create an animated 3D random walk plot using Matplotlib library in Python. We generated random walks and updated the plot for each frame of the animation. This technique can be used for visualizing particle movement, diffusion, and other stochastic processes in 3D space.

Other Python Tutorials you may like