{ "cells": [ { "cell_type": "markdown", "id": "38a53463-3e40-453c-ba66-412ddc59c0f5", "metadata": {}, "source": [ "## Basic Matplotlib interactive plot adjustable via widgets\n", "\n", "\n", "I was delighted to see that the newer JupyterLab seemed to have much better support for ipywidgets lately and so when I saw [this question](https://stackoverflow.com/q/67804804/8508004) at StackOverflow, I thought it was a good time to test how the solution proposed was working.\n", "In examining [the current answer](https://stackoverflow.com/a/67816794/8508004) to [this question](https://stackoverflow.com/q/67804804/8508004), I was finding the answer wasn't working in repositories where I had ipywidgets enabled, even when I tried in JupyterLab.\n", "\n", "However, the ipywidgets documentation includes [a matplotlib example](https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html?highlight=matplotlib#Flickering-and-jumping-output) (found by searching 'matplotlib' in the documentation).\n", "\n", "\n", "```python\n", "%matplotlib inline\n", "from ipywidgets import interactive\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "def f(m, b):\n", " plt.figure(2)\n", " x = np.linspace(-10, 10, num=1000)\n", " plt.plot(x, m * x + b)\n", " plt.ylim(-5, 5)\n", " plt.show()\n", "\n", "interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))\n", "output = interactive_plot.children[-1]\n", "output.layout.height = '350px'\n", "interactive_plot\n", "```\n", "\n", "I was able to take that and adapt @queezz's [solution](https://stackoverflow.com/a/67816794/8508004) to get it working.\n", "\n", "(Note when I did this, going to @queezz's repo [here](https://github.com/queezz/Complex_Numbers) and launching a binder session and opening the [MatplotliInteractTest.ipynb](https://github.com/queezz/Complex_Numbers/blob/3b15605d6edb63a833ca9357f6938b28c233314f/MatplotliInteractTest.ipynb) apparently meant to accompany, the answer, didn't allow the plot to work either. It said I suspect locking in the versions wasn't the best choice?)" ] }, { "cell_type": "code", "execution_count": 1, "id": "1071a693-a033-402f-ab61-1f376e03e75e", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ba083fe85f744084820b9ecdc713dcf2", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(IntSlider(value=5, description='w', max=10), FloatSlider(value=2.0, description='amp', m…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#%matplotlib inline # from the example in the documentation. but doesn't seem necessary in current JupyterLab 3.1.11 or the classic notebook available now https://github.com/fomightez/communication_voila\n", "from ipywidgets import interactive\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "def my_sine(x, w, amp, phi):\n", " \"\"\"\n", " Return a sine for x with angular frequency w and amplitude amp.\n", " \"\"\"\n", " return amp*np.sin(w * (x-phi))\n", "\n", "def f( w, amp, phi):\n", " plt.figure(2)\n", " x = np.linspace(0, 2 * np.pi, 300)\n", " plt.plot(x, my_sine(x, w, amp, phi), color='C0')\n", " #plt.ylim(-5, 5)\n", " plt.grid(True) #optional grid\n", " plt.show()\n", "\n", "interactive_plot = interactive(f, w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))\n", "#output = interactive_plot.children[-1]\n", "#output.layout.height = '450px'\n", "interactive_plot" ] }, { "cell_type": "code", "execution_count": null, "id": "41ac7707-8c7c-4766-9cc5-3190ff8e207d", "metadata": {}, "outputs": [], "source": [] } ], "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.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }