{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to use Matplotlib.animation's FuncAnimation and PyCav.display for inline notebook animation\n", "\n", "Matplotlib's animations when displayed inline by using %matplotlib notebook are very choppy and cannot be paused although they have the advantage of being displayed straight away. If a smooth animation is wanted along with the ability to pause then the PyCav module display can be used. This notebook will demonstrates its use, the documentation can also been found in the PyCav library documentation.\n", "\n", "As an example we shall animate a moving sin wave. We create a numpy array containing the data we require to produce the animation. Creating a figure and a function which steps through the time slices of the plot, we call the matplotlib FuncAnimation function, passing it the figure, the next frame function and the number of frames in the animation.\n", "\n", "### PyCav.display appears to work on Chrome and Firefox but has issues on Safari\n", "\n", "#### If you are writing new files you require the video encoder ffmpeg on your system" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#NAME: Inline Animation Tutorial\n", "#DESCRIPTION: How to use Matplotlib Animations and PyCav.display for inline notebook animation\n", "\n", "# Use these two lines on environments without displays i.e. servers\n", "#import matplotlib\n", "#matplotlib.use('Agg')\n", "\n", "import matplotlib.pyplot as plt\n", "import matplotlib.animation as anim\n", "\n", "import pycav.display as display\n", "\n", "import numpy as np\n", "\n", "x = np.linspace(0.,10.,101)\n", "t = np.linspace(0.,10.,51)\n", "\n", "sin_x = np.zeros((101,51))\n", "\n", "for i in range(51):\n", " sin_x[:,i] = np.sin(np.pi*x-np.pi*t[i])\n", "\n", "fig = plt.figure(figsize = (7,7))\n", "ax = plt.subplot(111)\n", "line = ax.plot(x,sin_x[:,0])[0]\n", " \n", "def nextframe(arg):\n", " line.set_data(x,sin_x[:,arg+1])\n", " \n", "animate1 = anim.FuncAnimation(fig,nextframe,interval = 100,frames = 50, repeat = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we are creating an animation which we do not want store in memory. We can use the temp = True argument to write the animation to a temporary file. The information about the produced video is appended to the animate object so we must pass this and set it as the output as well." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "animate1 = display.create_animation(animate1,temp = True)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can call the display_animation function which takes this animate object as the argument and displays the created video. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "display.display_animation(animate1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose now we wish to create a permanant file in the directory of the notebook you are working in. Now we must give create_animation the filename as a string. Now display_animation can be passed the file name as an argument and it will display it. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "animate1 = display.create_animation(animate1,fname = 'example.mp4')\n", "display.display_animation(animate1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will notice if you run the above cell twice it takes the same amount of time each time. This is because the file 'example.mp4' is being overwritten. If this is not desired then overwrite can be set to False. Then only if the filename does not exist will a new file will be created.\n", "\n", "The code below if run will not overwrite the example file so will very quickly display the example.mp4 animation" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "animate1 = display.create_animation(animate1,fname = 'example.mp4', overwrite = False)\n", "display.display_animation('example.mp4')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }