{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Attaching play buttons to sliders\n",
    "\n",
    ":::{note}\n",
    "\n",
    "The labels will not update as that requires a Python kernel.\n",
    "\n",
    ":::\n",
    "\n",
    "If you are working in Jupyter then you can add an [`ipywidgets.Play`](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html#Play-(Animation)-widget) widget to the sliders for any of the `interactive_*` functions.\n",
    "\n",
    "In this tutorial all the functions are {func}`~matplotlib.pyplot.scatter` but this will work for {func}`~matplotlib.pyplot.plot`, {func}`~matplotlib.pyplot.hist`, {func}`~matplotlib.pyplot.imshow`, etc..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Specifying Which Sliders Get Play buttons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib ipympl\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import mpl_interactions.ipyplot as iplt\n",
    "\n",
    "# turn off interactive mode so that broken\n",
    "# plots don't render in the docs\n",
    "plt.ioff()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### {obj}`bool`: All get a button"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 50\n",
    "x = np.random.rand(N)\n",
    "\n",
    "\n",
    "def f_y(x, tau, beta):\n",
    "    return np.sin(x * tau) ** 2 + np.random.randn(N) * 0.01 * beta\n",
    "\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### {obj}`str`: Position the button\n",
    "You make a play button for all sliders on either the left or the right using a string argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=\"left\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=\"right\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### {obj}`list`: Choose by name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "controls = iplt.scatter(x, f_y, tau=(1, 2 * np.pi, 100), beta=(0, 2), play_buttons=[\"tau\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### {obj}`~collections.defaultdict`: Specify by name and choose default\n",
    "\n",
    "If you have many parameters and you want the most, but not all, of them to have a Play button then\n",
    "you should use a {obj}`~collections.defaultdict`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "\n",
    "def f(x, **kwargs):\n",
    "    return x\n",
    "\n",
    "\n",
    "play_buttons = defaultdict(lambda: True)\n",
    "play_buttons[\"tau\"] = False\n",
    "fig, ax = plt.subplots()\n",
    "controls = iplt.scatter(\n",
    "    x,\n",
    "    f,\n",
    "    tau=(1, 2 * np.pi, 100),\n",
    "    beta=(0, 2),\n",
    "    zeta=(0, 1),\n",
    "    psi=(0, 1),\n",
    "    play_buttons=play_buttons,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
