|
354 | 354 | "interact(f, x=[('one', 10), ('two', 20)]);"
|
355 | 355 | ]
|
356 | 356 | },
|
357 |
| - { |
358 |
| - "cell_type": "code", |
359 |
| - "execution_count": null, |
360 |
| - "metadata": {}, |
361 |
| - "outputs": [], |
362 |
| - "source": [] |
363 |
| - }, |
364 | 357 | {
|
365 | 358 | "cell_type": "markdown",
|
366 | 359 | "metadata": {
|
|
370 | 363 | "## Basic interactive plot\n",
|
371 | 364 | "\n",
|
372 | 365 | "\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." |
376 | 367 | ]
|
377 | 368 | },
|
378 | 369 | {
|
|
385 | 376 | "source": [
|
386 | 377 | "import matplotlib.pyplot as plt\n",
|
387 | 378 | "import numpy as np\n",
|
| 379 | + "\n", |
388 | 380 | "def f(m, b):\n",
|
389 |
| - " plt.figure(2)\n", |
| 381 | + " fig = plt.figure()\n", |
390 | 382 | " plt.clf()\n",
|
391 | 383 | " plt.grid()\n",
|
392 | 384 | " x = np.linspace(-10, 10, num=1000)\n",
|
393 | 385 | " plt.plot(x, m * x + b)\n",
|
394 | 386 | " 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", |
413 | 389 | "interact(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))"
|
414 | 390 | ]
|
415 | 391 | },
|
|
420 | 396 | "## Fully interactive plot\n",
|
421 | 397 | "\n",
|
422 | 398 | "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", |
424 | 400 | "2. No zooming or panning\n",
|
425 | 401 | "3. Screen can jump when moving the sliders\n",
|
426 | 402 | "\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`." |
428 | 404 | ]
|
429 | 405 | },
|
430 | 406 | {
|
|
435 | 411 | },
|
436 | 412 | "outputs": [],
|
437 | 413 | "source": [
|
438 |
| - "# activate the widget based backend.\n", |
| 414 | + "# Activate the widget based backend.\n", |
439 | 415 | "%matplotlib ipympl\n",
|
440 | 416 | "\n",
|
441 | 417 | "x = np.linspace(-10, 10, num=1000)\n",
|
442 |
| - "m, b = 0, 0\n", |
443 |
| - "\n", |
444 | 418 | "fig, ax = plt.subplots()\n",
|
445 | 419 | "ax.grid()\n",
|
446 | 420 | "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", |
449 | 424 | "\n",
|
450 | 425 | "@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", |
453 | 427 | " line.set_ydata(m * x + b)\n",
|
454 |
| - " fig.canvas.draw_idle()\n" |
| 428 | + " # Request a widget redraw.\n", |
| 429 | + " fig.canvas.draw_idle()" |
455 | 430 | ]
|
456 | 431 | },
|
457 | 432 | {
|
|
460 | 435 | "source": [
|
461 | 436 | "## mpl-interactions\n",
|
462 | 437 | "\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." |
464 | 439 | ]
|
465 | 440 | },
|
466 | 441 | {
|
|
477 | 452 | "ax.grid()\n",
|
478 | 453 | "ax.set_ylim(-5,5)\n",
|
479 | 454 | "\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", |
481 | 456 | "def f(x, m, b):\n",
|
482 | 457 | " return m * x + b\n",
|
483 | 458 | " \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))" |
485 | 460 | ]
|
486 | 461 | },
|
487 | 462 | {
|
|
509 | 484 | " return np.sin(k*x -p)\n",
|
510 | 485 | "fig, ax = plt.subplots()\n",
|
511 | 486 | "ax.grid()\n",
|
512 |
| - "ax.plot(x, f(x, k, p))\n" |
| 487 | + "ax.plot(x, f(x, k, p))" |
513 | 488 | ]
|
514 | 489 | },
|
515 | 490 | {
|
|
0 commit comments