Animated Plots

Animated Plots#


usin Matplotlib’s animation package:

import numpy             as np
import matplotlib.pyplot as plt

import matplotlib
import matplotlib.animation as animation

# set the animation style to "jshtml" (for the use in Jupyter)
matplotlib.rcParams['animation.html'] = 'jshtml'
# create figure for the animation
fig = plt.figure(1)
plt.grid(True)
plt.xlim([0, 20])
plt.ylim([-2, 2])

# create a mass object for the animation
mass, = plt.plot(0, 0, 'o', markersize=12)

# prevent its showing
plt.close()

# define an animation function
def animate(f):
    # moving the mass
    mass.set_data([f], [np.sqrt(f)])

# create the animation
frames = np.arange(0, 200)

myAnimation = animation.FuncAnimation(fig, 
                                      animate, 
                                      frames, 
                                      interval = 10)
# show animation
myAnimation