{ "metadata": { "name": "animation_example" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "IPython Javascript Animation Plugin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Author: Jake Vanderplas:* [http://jakevdp.github.io](http://jakevdp.github.io)\n", "\n", "This is an example of embedding an animation via javascript into an IPython notebook.\n", "It requires the ``JSAnimation`` import, available at\n", "[http://github.com/jakevdp/JSAnimation](http://github.com/jakevdp/JSAnimation).\n", "\n", "The animation widget makes use of the HTML5 slider element, which is not yet supported by\n", "Firefox and some other browsers. For a comprehensive list of browser support for\n", "this element, see [http://caniuse.com/input-range](http://caniuse.com/input-range).\n", "\n", "This notebook contains a simple animation example which is displayed inline using the\n", "JSAnimation IPython_display plugin." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline].\n", "For more information, type 'help(pylab)'.\n" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# JSAnimation import available at https://github.com/jakevdp/JSAnimation\n", "from JSAnimation import IPython_display\n", "from matplotlib import animation\n", "\n", "# create a simple animation\n", "fig = plt.figure()\n", "ax = plt.axes(xlim=(0, 10), ylim=(-2, 2))\n", "line, = ax.plot([], [], lw=2)\n", "\n", "x = np.linspace(0, 10, 1000)\n", "\n", "def init():\n", " line.set_data([], [])\n", " return line,\n", "\n", "def animate(i):\n", " line.set_data(x, np.cos(i * 0.02 * np.pi) * np.sin(x - i * 0.02 * np.pi))\n", " return line,\n", "\n", "animation.FuncAnimation(fig, animate, init_func=init,\n", " frames=100, interval=20, blit=True)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " Once \n", " Loop \n", " Reflect \n", "
\n", "
\n", "\n", "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }