{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Multiple Axes animation\n\nThis example showcases:\n\n- how animation across multiple subplots works,\n- using a figure artist in the animation.\n\nOutput generated via `matplotlib.animation.Animation.to_jshtml`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport matplotlib.animation as animation\nfrom matplotlib.patches import ConnectionPatch\n\nfig, (axl, axr) = plt.subplots(\n ncols=2,\n sharey=True,\n figsize=(6, 2),\n gridspec_kw=dict(width_ratios=[1, 3], wspace=0),\n)\naxl.set_aspect(1)\naxr.set_box_aspect(1 / 3)\naxr.yaxis.set_visible(False)\naxr.xaxis.set_ticks([0, np.pi, 2 * np.pi], [\"0\", r\"$\\pi$\", r\"$2\\pi$\"])\n\n# draw circle with initial point in left Axes\nx = np.linspace(0, 2 * np.pi, 50)\naxl.plot(np.cos(x), np.sin(x), \"k\", lw=0.3)\npoint, = axl.plot(0, 0, \"o\")\n\n# draw full curve to set view limits in right Axes\nsine, = axr.plot(x, np.sin(x))\n\n# draw connecting line between both graphs\ncon = ConnectionPatch(\n (1, 0),\n (0, 0),\n \"data\",\n \"data\",\n axesA=axl,\n axesB=axr,\n color=\"C0\",\n ls=\"dotted\",\n)\nfig.add_artist(con)\n\n\ndef animate(i):\n x = np.linspace(0, i, int(i * 25 / np.pi))\n sine.set_data(x, np.sin(x))\n x, y = np.cos(i), np.sin(i)\n point.set_data([x], [y])\n con.xy1 = x, y\n con.xy2 = i, y\n return point, sine, con\n\n\nani = animation.FuncAnimation(\n fig,\n animate,\n interval=50,\n blit=False, # blitting can't be used with Figure artists\n frames=x,\n repeat_delay=100,\n)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.patches.ConnectionPatch`\n - `matplotlib.animation.FuncAnimation`\n\n.. tags:: component: axes, animation\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }