|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Saving Animations\n", |
| 8 | + "\n", |
| 9 | + "Since the controls object knows how to update figures as the sliders change their values it is also able to save an animation (e.g. `.gif` or `.mp4` by updating the slider values for you. Under the hood this makes use of [FuncAnimation](https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html?highlight=funcanimation#matplotlib.animation.FuncAnimation) and you can pass any relevant kwargs in via `func_anim_kwargs`. Other `kwargs` will passed to `animation.save`.\n", |
| 10 | + "\n", |
| 11 | + "Saving animations will work with either ipywidgets Sliders or with matplotlib Sliders. However, it will not work with other widgets. (This is an potential area of improvement, PRs welcome)" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": null, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "%matplotlib ipympl\n", |
| 21 | + "import matplotlib.pyplot as plt\n", |
| 22 | + "import numpy as np\n", |
| 23 | + "\n", |
| 24 | + "import mpl_interactions.ipyplot as iplt" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## Basic Usage" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": null, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "x = np.linspace(0, 2 * np.pi, 200)\n", |
| 41 | + "\n", |
| 42 | + "\n", |
| 43 | + "def f(x, amp, freq):\n", |
| 44 | + " return amp * np.sin(x * freq)\n", |
| 45 | + "\n", |
| 46 | + "\n", |
| 47 | + "# Create the plot as normal\n", |
| 48 | + "fig, ax = plt.subplots()\n", |
| 49 | + "controls = iplt.plot(x, f, freq=(0.05, 10, 250), amp=(1,10))\n", |
| 50 | + "_ = iplt.title(\"the Frequency is: {freq:.2f}\", controls=controls[\"freq\"])" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "# save as a gif\n", |
| 60 | + "anim = controls.save_animation(\"freq-plot-1.gif\", fig, \"freq\", interval=35)" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "markdown", |
| 65 | + "metadata": {}, |
| 66 | + "source": [ |
| 67 | + "### Which Generates this GIF\n", |
| 68 | + "\n", |
| 69 | + "" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "markdown", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "## Embeding the animation in a noteook.\n", |
| 77 | + "\n", |
| 78 | + "To embed the animation you can do:\n", |
| 79 | + "\n", |
| 80 | + "1. Link to it in markdown cell with ``\n", |
| 81 | + "2. Drag the file into a markdown cell\n", |
| 82 | + "3. Embed `anim.to_html5_video()` using IPython.display.Video:\n", |
| 83 | + "```python\n", |
| 84 | + "from IPython.display import Video\n", |
| 85 | + "Video(anim.to_html5_video(), embed=True)\n", |
| 86 | + "```\n", |
| 87 | + "\n", |
| 88 | + "4. Use IPython to display the saved gif\n", |
| 89 | + "\n", |
| 90 | + "You can also read more in this excellent blog post: http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-as-interactive-javascript-widgets/" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "# NBVAL_IGNORE_OUTPUT\n", |
| 100 | + "from IPython.display import Image\n", |
| 101 | + "\n", |
| 102 | + "Image(\"freq-plot-1.gif\")" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "## Matplotlib Sliders with `valstep=None`\n", |
| 110 | + "\n", |
| 111 | + "Matplotlib sliders have an optional attribute `valstep` that allows for discrete slider steps. `mpl-interactions` uses this for all sliders that it creates, however if you passed a custom made slider in as a kwarg you may not have used `valstep` if this is the case then the `save_animation` function cannot infer how many frames it should render, so you can specify this with the `N_frames` arguments." |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "from matplotlib.widgets import Slider\n", |
| 121 | + "\n", |
| 122 | + "import mpl_interactions.ipyplot as iplt\n", |
| 123 | + "\n", |
| 124 | + "fig, ax = plt.subplots()\n", |
| 125 | + "plt.subplots_adjust(bottom=0.25)\n", |
| 126 | + "x = np.linspace(0, 2 * np.pi, 200)\n", |
| 127 | + "\n", |
| 128 | + "\n", |
| 129 | + "def f(x, freq):\n", |
| 130 | + " return np.sin(x * freq)\n", |
| 131 | + "\n", |
| 132 | + "\n", |
| 133 | + "axfreq = plt.axes([0.25, 0.1, 0.65, 0.03])\n", |
| 134 | + "slider = Slider(axfreq, label=\"freq\", valmin=0.05, valmax=10) # note the lack of valstep\n", |
| 135 | + "controls2 = iplt.plot(x, f, freq=slider, ax=ax)\n", |
| 136 | + "_ = iplt.title(\"the Frequency is: {freq:.2f}\", controls=controls2[\"freq\"])" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": null, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "# save as a gif\n", |
| 146 | + "anim2 = controls2.save_animation(\"freq-plot-2.gif\", fig, \"freq\", interval=35, N_frames=100)" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "### Gives this GIF:\n", |
| 154 | + "\n", |
| 155 | + "" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [], |
| 163 | + "source": [] |
| 164 | + } |
| 165 | + ], |
| 166 | + "metadata": { |
| 167 | + "kernelspec": { |
| 168 | + "display_name": "Python 3", |
| 169 | + "language": "python", |
| 170 | + "name": "python3" |
| 171 | + }, |
| 172 | + "language_info": { |
| 173 | + "codemirror_mode": { |
| 174 | + "name": "ipython", |
| 175 | + "version": 3 |
| 176 | + }, |
| 177 | + "file_extension": ".py", |
| 178 | + "mimetype": "text/x-python", |
| 179 | + "name": "python", |
| 180 | + "nbconvert_exporter": "python", |
| 181 | + "pygments_lexer": "ipython3", |
| 182 | + "version": "3.9.0" |
| 183 | + } |
| 184 | + }, |
| 185 | + "nbformat": 4, |
| 186 | + "nbformat_minor": 4 |
| 187 | +} |
0 commit comments