We can use html5 for animation, as suggested in this web page: http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/
This seems to be the modern way to do animation in Python.
First step is to initialize some things in Python. - we need %matplotlib inline to get things to plot right on the notebook
We get all these with the following imports:
In [1]:
%matplotlib inline
from numpy import *
from matplotlib.pyplot import *
from matplotlib import animation
from IPython.display import HTML
We now have four steps to get an animated plot
Then we are ready to call “HTML” to display the animation.
In [2]:
# First set up the figure, the axis, and the plot element we want to animate
fig, ax = subplots()
ax.set_xlim(( 0, 2))
ax.set_ylim((-2, 2))
line, = ax.plot([], [], lw=2)
# NOTE: THIS WILL DISPLAY JUST AN EMPTY BOX.
In [3]:
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return (line,)
In [4]:
# animation function. This is called sequentially
def animate(i):
x = linspace(0, 2, 1000)
y = sin(2 * pi * (x - 0.01 * i))
line.set_data(x, y)
return (line,)
In [5]:
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
In [6]:
HTML(anim.to_html5_video())
Out[6]:
In [ ]: