# Make a movie out of the steps of a 2D random walk.
import matplotlib.pyplot as plt
from matplotlib import animation
from numpy.random import random as rand
# Set number of steps for each random walk.
num_steps = 1000
# Create an empty figure of the desired size.
plt.close('all')
# Clear anything left over from prior runs.
bound = 20
fig = plt.figure()
# Must have figure object for movie.
ax = plt.axes(xlim=(-bound, bound), ylim=(-bound, bound))
# Create empty line and point objects with no data.
# They will be updated during each frame of the animation.
(my_line,) = ax.plot([], [], lw=2)
# Line to show path
(my_point,) = ax.plot([], [], 'ro', ms=9)
# Dot to show current position
# Generate the random walk data.
x_steps = 2*(rand(num_steps) < 0.5) - 1
# Generate random steps +/- 1.
y_steps = 2*(rand(num_steps) < 0.5) - 1
x_coordinate = x_steps.cumsum()
# Sum steps to get position.
y_coordinate = y_steps.cumsum()
# This function will generate each frame of the animation.
# It adds all of the data through frame n to a line
# and moves a point to the nth position of the walk.
def get_step(n, x, y, this_line, this_point):
    this_line.set_data(x[:n+1], y[:n+1])
    this_point.set_data(x[n], y[n])

# Call the animator and create the movie.
my_movie = animation.FuncAnimation(fig, get_step, frames=num_steps,
                                   fargs=(x_coordinate, y_coordinate, my_line, my_point) )
# Install FFMPEG
my_movie.save('ex188_random_walk.mp4', fps=30)

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading