Simple Animation

11.1. Simple Animation#

Here is a simple animation borrowed at:

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

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])

def animate(i):
    l.set_data(t[:i], x[:i])

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))
plt.close() # Close the figure to prevent it from displaying in a static form

# Display the animation in a Jupyter notebook
from IPython.display import HTML
HTML(ani.to_jshtml())