{ "cells": [ { "cell_type": "markdown", "id": "2533ac08-4579-4f44-b4e9-04cdf6b2ec0d", "metadata": {}, "source": [ "# Animations with Matplotlib\n", "\n", "This notebook shows how to use the `FuncAnimation` function from the `matplotlib.animation` module to create animated plots. " ] }, { "cell_type": "markdown", "id": "a25dfb9e-c13a-4368-b78e-99b21e8719d5", "metadata": {}, "source": [ "## Animation Basics\n", "\n", "Before we dive into more complex example. it is helpful to understand the basics of matplotlib animation." ] }, { "cell_type": "markdown", "id": "59f6a24c-aa0b-428e-987c-b8c6a5303344", "metadata": {}, "source": [ "Let's define 3 positions and we will create an animation of a point moving between them." ] }, { "cell_type": "code", "execution_count": 1, "id": "7352f24a-9346-472e-b0ff-82b5dd143f2d", "metadata": {}, "outputs": [], "source": [ "points = [(0.1, 0.5), (0.5, 0.5), (0.9, 0.5)]" ] }, { "cell_type": "markdown", "id": "dc5700d6-31c8-47ee-a839-ea33dde9776b", "metadata": {}, "source": [ "Then we use the `FuncAnimation` class which makes an animation by repeatedly calling a function and saving the output as a frame in the animation." ] }, { "cell_type": "code", "execution_count": 2, "id": "f525f5cb-52a4-411d-b80f-0a93f48ccb0b", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from matplotlib.animation import FuncAnimation" ] }, { "cell_type": "markdown", "id": "ee2b1f7e-7bea-49d9-97ea-f8624c1534fa", "metadata": {}, "source": [ "We need to define a function that takes the frame number and generates a plot from it. Here we define a function `animation` that takes the frame index and creates a plot from the point at the same index in the `points` list. So at frame 0, it will display the first point, frame 1 the second point and so on." ] }, { "cell_type": "code", "execution_count": 3, "id": "bccd980f-bcc8-40b7-978b-522460120499", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(1, 1)\n", "fig.set_size_inches(5,5)\n", "fig.tight_layout(rect=[0, 0.03, 1, 0.95])\n", "\n", "def animate(i):\n", " ax.clear()\n", " # Get the point from the points list at index i\n", " point = points[i]\n", " # Plot that point using the x and y coordinates\n", " ax.plot(point[0], point[1], color='green', \n", " label='original', marker='o')\n", " # Set the x and y axis to display a fixed range\n", " ax.set_xlim([0, 1])\n", " ax.set_ylim([0, 1])\n", "ani = FuncAnimation(fig, animate, frames=len(points),\n", " interval=500, repeat=False)\n", "plt.close()" ] }, { "cell_type": "markdown", "id": "3dbd4619-6dd3-4056-a4ed-75ae94fa6c68", "metadata": {}, "source": [ "The animation is now contained in the `ani` object. We can call `save()` and save the result as an animated GIF. We need to specify a `writer` that supports the output format." ] }, { "cell_type": "code", "execution_count": 4, "id": "fe9cf429-a1cd-4615-95c5-bed9acdc3896", "metadata": {}, "outputs": [], "source": [ "from matplotlib.animation import PillowWriter\n", "# Save the animation as an animated GIF\n", "ani.save(\"simple_animation.gif\", dpi=300,\n", " writer=PillowWriter(fps=1))" ] }, { "cell_type": "markdown", "id": "91491d39-2a07-4b37-8ee3-d229286efa9e", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "id": "728bd5c8-ddbc-46de-84ba-a05255341551", "metadata": {}, "source": [ "We can also use the `to_jshtml()` function to create an HTML representation of the animation and display in a Jupyter notebook." ] }, { "cell_type": "code", "execution_count": 5, "id": "065d909b-2c7a-441f-a9f7-cdfabc6be6cc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "