Skip to content

Commit 27b0bb3

Browse files
bsyounessianhi
authored andcommitted
Small changes
1 parent cf78313 commit 27b0bb3

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

notebooks/02.Interact/02.00-Using-Interact.ipynb

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,6 @@
354354
"interact(f, x=[('one', 10), ('two', 20)]);"
355355
]
356356
},
357-
{
358-
"cell_type": "code",
359-
"execution_count": null,
360-
"metadata": {},
361-
"outputs": [],
362-
"source": []
363-
},
364357
{
365358
"cell_type": "markdown",
366359
"metadata": {
@@ -370,9 +363,7 @@
370363
"## Basic interactive plot\n",
371364
"\n",
372365
"\n",
373-
"The function below plots a straight line whose slope and intercept are given by its arguments.\n",
374-
"\n",
375-
"The interactive below displays a line whose slope and intercept is set by the sliders. Note that if the variable containing the widget, `interactive_plot`, is the last thing in the cell it is displayed."
366+
"The function below plots a straight line whose slope and intercept are given by its arguments."
376367
]
377368
},
378369
{
@@ -385,31 +376,16 @@
385376
"source": [
386377
"import matplotlib.pyplot as plt\n",
387378
"import numpy as np\n",
379+
"\n",
388380
"def f(m, b):\n",
389-
" plt.figure(2)\n",
381+
" fig = plt.figure()\n",
390382
" plt.clf()\n",
391383
" plt.grid()\n",
392384
" x = np.linspace(-10, 10, num=1000)\n",
393385
" plt.plot(x, m * x + b)\n",
394386
" plt.ylim(-5, 5)\n",
395-
" plt.show()"
396-
]
397-
},
398-
{
399-
"cell_type": "markdown",
400-
"metadata": {},
401-
"source": [
402-
"The interactive below displays a line whose slope and intercept is set by the sliders."
403-
]
404-
},
405-
{
406-
"cell_type": "code",
407-
"execution_count": null,
408-
"metadata": {
409-
"tags": []
410-
},
411-
"outputs": [],
412-
"source": [
387+
" plt.show()\n",
388+
"\n",
413389
"interact(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))"
414390
]
415391
},
@@ -420,11 +396,11 @@
420396
"## Fully interactive plot\n",
421397
"\n",
422398
"While the above example works, it has some drawbacks:\n",
423-
"1. It is inefficient to re-run all the plotting code\n",
399+
"1. It is inefficient to re-run all the plotting code (the whole plot gets re-created every time -- to see this, run the code above again after running the cell below and see how the figure numbers get incremented)\n",
424400
"2. No zooming or panning\n",
425401
"3. Screen can jump when moving the sliders\n",
426402
"\n",
427-
"A better solution is to use the [ipympl](https://matplotlib.org/ipympl/) Matplotlib backend. You can activate this with the line magic: `%matplotlib ipympl`. Then in your interactive function you update the matplotlib artists."
403+
"A better solution is to use the [ipympl](https://matplotlib.org/ipympl/) Matplotlib backend. You can activate this with the line magic: `%matplotlib ipympl`."
428404
]
429405
},
430406
{
@@ -435,23 +411,22 @@
435411
},
436412
"outputs": [],
437413
"source": [
438-
"# activate the widget based backend.\n",
414+
"# Activate the widget based backend.\n",
439415
"%matplotlib ipympl\n",
440416
"\n",
441417
"x = np.linspace(-10, 10, num=1000)\n",
442-
"m, b = 0, 0\n",
443-
"\n",
444418
"fig, ax = plt.subplots()\n",
445419
"ax.grid()\n",
446420
"ax.set_ylim(-5, 5)\n",
447-
"line = ax.plot(x, m * x + b)[0]\n",
448-
"\n",
421+
"# Initialize a plot object with y = x. We'll be modifying y below.\n",
422+
"# This returns a list of `.Line2D` representing the plotted data. We grab the first one -- we only have 1 series.\n",
423+
"line = ax.plot(x, x)[0]\n",
449424
"\n",
450425
"@interact(m=(-2.0, 2.0), b=(-3, 3, 0.5))\n",
451-
"def update_line(m, b):\n",
452-
" # use matplotlib functions to update the canvas\n",
426+
"def update_line(m=1, b=0.5):\n",
453427
" line.set_ydata(m * x + b)\n",
454-
" fig.canvas.draw_idle()\n"
428+
" # Request a widget redraw.\n",
429+
" fig.canvas.draw_idle()"
455430
]
456431
},
457432
{
@@ -460,7 +435,7 @@
460435
"source": [
461436
"## mpl-interactions\n",
462437
"\n",
463-
"The [mpl-interactions](https://mpl-interactions.readthedocs.io/en/stable/) library can automate the updating of Matplotlib artists for you. non-matplotlib kwargs to functions like `plot` will be interpted to sliders and widget controls similarly to `interact` and automatically connected with the matplotlib artists."
438+
"The [mpl-interactions](https://mpl-interactions.readthedocs.io/en/stable/) library can automate the updating of Matplotlib plots for you. Non-matplotlib keyword arguments passed to functions like `plot` will be interpreted to sliders and widget controls, similarly to `interact`, and automatically connected with the matplotlib plots."
464439
]
465440
},
466441
{
@@ -477,11 +452,11 @@
477452
"ax.grid()\n",
478453
"ax.set_ylim(-5,5)\n",
479454
"\n",
480-
"# define function in a way you can re-use in calculations\n",
455+
"# Define function in a way you can re-use in calculations\n",
481456
"def f(x, m, b):\n",
482457
" return m * x + b\n",
483458
" \n",
484-
"ctrls = iplt.plot(x, f, m = (-2,2), b=(-3, 3, 10))\n"
459+
"ctrls = iplt.plot(x, f, m=(-2,2), b=(-3, 3, 10))"
485460
]
486461
},
487462
{
@@ -509,7 +484,7 @@
509484
" return np.sin(k*x -p)\n",
510485
"fig, ax = plt.subplots()\n",
511486
"ax.grid()\n",
512-
"ax.plot(x, f(x, k, p))\n"
487+
"ax.plot(x, f(x, k, p))"
513488
]
514489
},
515490
{

0 commit comments

Comments
 (0)