{ "cells": [ { "cell_type": "markdown", "id": "9bdc7777-5241-4944-a12b-46f05c894dbe", "metadata": {}, "source": [ "# Contextmanager for Controls object\n", "\n", "\n", "When you are making a complex interactive visualization you may need to use many `mpl-interactions` functions. One way to do this is to pass `controls=controls` to every function you call. But that's pretty lame because you have to type the same thing out many times. So you can also use `controls` objects as context managers:" ] }, { "cell_type": "code", "execution_count": null, "id": "4c34e1ff-3ef3-4090-a2bd-7dcb3701e58b", "metadata": { "tags": [] }, "outputs": [], "source": [ "%matplotlib ipympl" ] }, { "cell_type": "markdown", "id": "28ae4a71-a451-4846-be37-161a42d4a18e", "metadata": {}, "source": [ "## Using all params\n", "\n", "If you want to use all the parameters in a controls object then you can simply do:\n", "```python\n", "with controls:\n", " # iplt functions here\n", "```\n", "\n", "## Using only a subset of the control's params\n", "You can also use a subset of the params in a context by indexing it with strings.\n", "\n", "```python\n", "\n", "with controls['param-name', 'param-name-2',...]:\n", " # iplt functions here\n", "```\n", "\n", "## Adding new params from within a context\n", "\n", "You can still add new params from within a controls context:\n", "\n", "```python\n", "with controls:\n", " iplt.plot(x,f, new_param=np.linspace(0, 1))\n", "```\n", "\n", "\n", "## Example\n", "\n", "Here is an example that demonstrates the various possible behaviors" ] }, { "cell_type": "code", "execution_count": null, "id": "852d736e-2839-4b22-9bd5-1e7b4e35a1d9", "metadata": { "tags": [] }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "import mpl_interactions.ipyplot as iplt\n", "\n", "x = np.linspace(0, 2 * np.pi, 1000)\n", "tau = np.linspace(5, 10)\n", "beta = np.linspace(0.25, 1)\n", "delta = np.linspace(0, 0.75)\n", "fig, ax = plt.subplots()\n", "\n", "\n", "def f1(tau, beta):\n", " return np.sin(x * tau) * x * beta\n", "\n", "\n", "def f2(tau, beta):\n", " return np.sin(x * beta) * x * tau\n", "\n", "\n", "def f2_tau(tau):\n", " return np.sin(x * 1) * x * tau\n", "\n", "\n", "def f2_tau_delta(tau, delta):\n", " return np.sin(x * delta) * x * tau\n", "\n", "\n", "# Create the initial object\n", "controls = iplt.plot(f1, \"k--\", tau=tau, beta=beta, label=\"f1\")\n", "\n", "# context with all parameters\n", "with controls:\n", " iplt.plot(f2, label=\"f2\")\n", "\n", "# adding a new parameter and excluding beta\n", "with controls[\"tau\"]:\n", " iplt.plot(f2_tau_delta, delta=delta, label=\"f2 tau alpha\")\n", "\n", "# excluding all params except tau\n", "with controls[\"tau\"]:\n", " iplt.plot(f2_tau, label=\"f2 only tau\")\n", "\n", "_ = plt.legend()" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }