IPython Javascript Animation Plugin

Author: Jake Vanderplas: http://jakevdp.github.io

This is an example of embedding an animation via javascript into an IPython notebook. It requires the JSAnimation import, available at http://github.com/jakevdp/JSAnimation.

The animation widget makes use of the HTML5 slider element, which is not yet supported by Firefox and some other browsers. For a comprehensive list of browser support for this element, see http://caniuse.com/input-range.

This notebook contains a simple animation example which is displayed inline using the JSAnimation IPython_display plugin.


In [1]:
%pylab inline


Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.

In [2]:
# JSAnimation import available at https://github.com/jakevdp/JSAnimation
from JSAnimation import IPython_display
from matplotlib import animation

# create a simple animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

x = np.linspace(0, 10, 1000)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(x, np.cos(i * 0.02 * np.pi) * np.sin(x - i * 0.02 * np.pi))
    return line,

animation.FuncAnimation(fig, animate, init_func=init,
                        frames=100, interval=20, blit=True)


Out[2]:


Once Loop Reflect