{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Animated scatter saved as GIF\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\n\nfig, ax = plt.subplots()\nax.set_xlim([0, 10])\n\nscat = ax.scatter(1, 0)\nx = np.linspace(0, 10)\n\n\ndef animate(i):\n scat.set_offsets((x[i], 0))\n return (scat,)\n\n\nani = animation.FuncAnimation(fig, animate, repeat=True, frames=len(x) - 1, interval=50)\n\n# To save the animation using Pillow as a gif\n# writer = animation.PillowWriter(fps=15,\n# metadata=dict(artist='Me'),\n# bitrate=1800)\n# ani.save('scatter.gif', writer=writer)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n component: animation,\n plot-type: scatter,\n purpose: reference,\n level: intermediate\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 }