diff --git a/ipywidgets-tutorial.ipynb b/ipywidgets-tutorial.ipynb index 4e04684..4013ce6 100644 --- a/ipywidgets-tutorial.ipynb +++ b/ipywidgets-tutorial.ipynb @@ -183,22 +183,21 @@ "ax.set_ylim([-4, 4])\n", "ax.grid(True)\n", "\n", - "# generate x values\n", + "# generate x values and create placeholder plot\n", "x = np.linspace(0, 2 * np.pi, 100)\n", - "\n", + "scope = ax.plot(x, x, color='C0')\n", "\n", "def my_sine(x, w, amp, phi):\n", " \"\"\"\n", - " Return a sine for x with angular frequeny w and amplitude amp.\n", + " Return a sine for x with angular frequency w and amplitude amp.\n", " \"\"\"\n", " return amp*np.sin(w * (x-phi))\n", "\n", "\n", "@widgets.interact(w=(0, 10, 1), amp=(0, 4, .1), phi=(0, 2*np.pi+0.01, 0.01))\n", - "def update(w = 1.0, amp=1, phi=0):\n", - " \"\"\"Remove old lines from plot and plot new one\"\"\"\n", - " [l.remove() for l in ax.lines]\n", - " ax.plot(x, my_sine(x, w, amp, phi), color='C0')" + "def update(w=1.0, amp=1, phi=0):\n", + " \"\"\"Replace data of plot with new one\"\"\"\n", + " scope[0].set_ydata(my_sine(x, w, amp, phi))\n" ] }, {