| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Updating Plots" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "markdown", |
| 12 |
"metadata": {}, |
| 13 |
"source": [ |
| 14 |
"`bqplot` is an interactive plotting library. Attributes of plots can be updated in place without recreating the whole figure and marks. Let's look at idiomatic ways of updating plots in `bqplot`" |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "code", |
| 19 |
"execution_count": null, |
| 20 |
"metadata": {}, |
| 21 |
"outputs": [], |
| 22 |
"source": [ |
| 23 |
"import numpy as np\n", |
| 24 |
"import bqplot.pyplot as plt" |
| 25 |
] |
| 26 |
}, |
| 27 |
{ |
| 28 |
"cell_type": "code", |
| 29 |
"execution_count": null, |
| 30 |
"metadata": {}, |
| 31 |
"outputs": [], |
| 32 |
"source": [ |
| 33 |
"x = np.linspace(-10, 10, 100)\n", |
| 34 |
"y = np.sin(x)\n", |
| 35 |
"\n", |
| 36 |
"fig = plt.figure()\n", |
| 37 |
"line = plt.plot(x=x, y=y)\n", |
| 38 |
"fig" |
| 39 |
] |
| 40 |
}, |
| 41 |
{ |
| 42 |
"cell_type": "markdown", |
| 43 |
"metadata": {}, |
| 44 |
"source": [ |
| 45 |
"To update the attributes of the plot(x, y, color etc.) the correct way to do it is to update the attributes of the **mark** objects in place. Recreating figure or mark objects is not recommended" |
| 46 |
] |
| 47 |
}, |
| 48 |
{ |
| 49 |
"cell_type": "code", |
| 50 |
"execution_count": null, |
| 51 |
"metadata": {}, |
| 52 |
"outputs": [], |
| 53 |
"source": [ |
| 54 |
"# update y attribute of the line object\n", |
| 55 |
"line.y = np.tan(x)" |
| 56 |
] |
| 57 |
}, |
| 58 |
{ |
| 59 |
"cell_type": "markdown", |
| 60 |
"metadata": {}, |
| 61 |
"source": [ |
| 62 |
"We can update multiple attributes of the mark object simultaneously by using the `hold_sync` method like so. (This makes only one round trip from the python kernel to front end)" |
| 63 |
] |
| 64 |
}, |
| 65 |
{ |
| 66 |
"cell_type": "code", |
| 67 |
"execution_count": null, |
| 68 |
"metadata": {}, |
| 69 |
"outputs": [], |
| 70 |
"source": [ |
| 71 |
"# update both x and y together\n", |
| 72 |
"with line.hold_sync():\n", |
| 73 |
" line.x = np.arange(100)\n", |
| 74 |
" line.y = x ** 3 - x" |
| 75 |
] |
| 76 |
}, |
| 77 |
{ |
| 78 |
"cell_type": "markdown", |
| 79 |
"metadata": {}, |
| 80 |
"source": [ |
| 81 |
"We can also animate the changes to the x, y and other data attributes by setting the `animation_duration` property on the figure object. More examples of animations can found in the [Animations](../Advanced Plotting/Animations.ipynb) notebook" |
| 82 |
] |
| 83 |
}, |
| 84 |
{ |
| 85 |
"cell_type": "code", |
| 86 |
"execution_count": null, |
| 87 |
"metadata": {}, |
| 88 |
"outputs": [], |
| 89 |
"source": [ |
| 90 |
"fig.animation_duration = 1000\n", |
| 91 |
"line.y = np.cos(x)" |
| 92 |
] |
| 93 |
}, |
| 94 |
{ |
| 95 |
"cell_type": "markdown", |
| 96 |
"metadata": {}, |
| 97 |
"source": [ |
| 98 |
"Let's look at an example to update a scatter plot" |
| 99 |
] |
| 100 |
}, |
| 101 |
{ |
| 102 |
"cell_type": "code", |
| 103 |
"execution_count": null, |
| 104 |
"metadata": {}, |
| 105 |
"outputs": [], |
| 106 |
"source": [ |
| 107 |
"x, y = np.random.rand(2, 10)\n", |
| 108 |
"\n", |
| 109 |
"fig = plt.figure(animation_duration=1000)\n", |
| 110 |
"scat = plt.scatter(x=x, y=y)\n", |
| 111 |
"fig" |
| 112 |
] |
| 113 |
}, |
| 114 |
{ |
| 115 |
"cell_type": "code", |
| 116 |
"execution_count": null, |
| 117 |
"metadata": {}, |
| 118 |
"outputs": [], |
| 119 |
"source": [ |
| 120 |
"# update the x and y attreibutes in place using hold_sync\n", |
| 121 |
"with scat.hold_sync():\n", |
| 122 |
" scat.x, scat.y = np.random.rand(2, 10)" |
| 123 |
] |
| 124 |
} |
| 125 |
], |
| 126 |
"metadata": { |
| 127 |
"kernelspec": { |
| 128 |
"display_name": "Python 3", |
| 129 |
"language": "python", |
| 130 |
"name": "python3" |
| 131 |
}, |
| 132 |
"language_info": { |
| 133 |
"codemirror_mode": { |
| 134 |
"name": "ipython", |
| 135 |
"version": 3 |
| 136 |
}, |
| 137 |
"file_extension": ".py", |
| 138 |
"mimetype": "text/x-python", |
| 139 |
"name": "python", |
| 140 |
"nbconvert_exporter": "python", |
| 141 |
"pygments_lexer": "ipython3", |
| 142 |
"version": "3.7.6" |
| 143 |
} |
| 144 |
}, |
| 145 |
"nbformat": 4, |
| 146 |
"nbformat_minor": 1 |
| 147 |
} |