| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Selectors\n" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "markdown", |
| 12 |
"metadata": {}, |
| 13 |
"source": [ |
| 14 |
"## Index\n", |
| 15 |
"\n", |
| 16 |
"- [Introduction](#introduction)\n", |
| 17 |
"- [Brush Selectors](#brushselectors)\n", |
| 18 |
"- [FastIntervalSelector](#fastintervalselector)\n", |
| 19 |
"- [LassoSelector](#lassoselector)\n", |
| 20 |
"- [IndexSelector](#indexselector)\n", |
| 21 |
"- [MultiSelector](#multiselector)" |
| 22 |
] |
| 23 |
}, |
| 24 |
{ |
| 25 |
"cell_type": "code", |
| 26 |
"execution_count": null, |
| 27 |
"metadata": {}, |
| 28 |
"outputs": [], |
| 29 |
"source": [ |
| 30 |
"import pandas as pd\n", |
| 31 |
"import numpy as np\n", |
| 32 |
"\n", |
| 33 |
"symbol = 'Security 1'\n", |
| 34 |
"symbol2 = 'Security 2'" |
| 35 |
] |
| 36 |
}, |
| 37 |
{ |
| 38 |
"cell_type": "code", |
| 39 |
"execution_count": null, |
| 40 |
"metadata": {}, |
| 41 |
"outputs": [], |
| 42 |
"source": [ |
| 43 |
"price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.4], [0.4, 1.0]]), axis=0) + 100,\n", |
| 44 |
" columns=[symbol, symbol2],\n", |
| 45 |
" index=pd.date_range(start='01-01-2007', periods=150))\n", |
| 46 |
"\n", |
| 47 |
"dates_actual = price_data.index.values\n", |
| 48 |
"prices = price_data[symbol].values" |
| 49 |
] |
| 50 |
}, |
| 51 |
{ |
| 52 |
"cell_type": "code", |
| 53 |
"execution_count": null, |
| 54 |
"metadata": {}, |
| 55 |
"outputs": [], |
| 56 |
"source": [ |
| 57 |
"from bqplot import *\n", |
| 58 |
"from bqplot.interacts import (\n", |
| 59 |
" FastIntervalSelector, IndexSelector, BrushIntervalSelector,\n", |
| 60 |
" BrushSelector, MultiSelector, LassoSelector,\n", |
| 61 |
")\n", |
| 62 |
"\n", |
| 63 |
"from ipywidgets import ToggleButtons, VBox, HTML" |
| 64 |
] |
| 65 |
}, |
| 66 |
{ |
| 67 |
"cell_type": "markdown", |
| 68 |
"metadata": {}, |
| 69 |
"source": [ |
| 70 |
"## Introduction <a class=\"anchor\" id=\"introduction\"></a>\n", |
| 71 |
"\n", |
| 72 |
"Selectors are part of the Interaction Layer (link).\n", |
| 73 |
"They are used to select subparts of Marks, that correspond to different regions on the Figure canvas. Different types of selectors select different types of regions:\n", |
| 74 |
"- `BrushSelector`, `FastIntervalSelector` and `MultiSelector` select rectangular regions\n", |
| 75 |
"- `IndexSelector` selects the elements closest to an abcissa\n", |
| 76 |
"- `LassoSelector` selects elements in a region drawn by the user\n", |
| 77 |
"\n", |
| 78 |
"## How they work\n", |
| 79 |
"\n", |
| 80 |
"bqplot Selectors need to be tied to two other widgets:\n", |
| 81 |
"- One or several marks. Their `selected` attribute, a list of data indices, will be set by the `Selector` instance.\n", |
| 82 |
"- One (1d selection) or two (2d selection) `Scales`. These are the scales that the `Selector` operates on. The `Selector`'s `selected` attribute will be expressed as values of those scales.\n", |
| 83 |
"\n", |
| 84 |
"The `Selector` must then be passed to the desired `Figure`, as its `interaction` attribute.\n", |
| 85 |
"\n", |
| 86 |
"Hopefully this will be clear in the following examples." |
| 87 |
] |
| 88 |
}, |
| 89 |
{ |
| 90 |
"cell_type": "code", |
| 91 |
"execution_count": null, |
| 92 |
"metadata": {}, |
| 93 |
"outputs": [], |
| 94 |
"source": [ |
| 95 |
"# Define scales for the rest of the notebook\n", |
| 96 |
"scales = {'x': DateScale(), 'y': LinearScale()}" |
| 97 |
] |
| 98 |
}, |
| 99 |
{ |
| 100 |
"cell_type": "markdown", |
| 101 |
"metadata": {}, |
| 102 |
"source": [ |
| 103 |
"Brush Selectors <a class=\"anchor\" id=\"brushselectors\"></a>\n", |
| 104 |
"--------------------\n", |
| 105 |
"\n", |
| 106 |
"Selects a rectangular region of the `Figure`.\n", |
| 107 |
"\n", |
| 108 |
"#### Usage:\n", |
| 109 |
"- Click and drag to create a new brush\n", |
| 110 |
"- Drag the edge of the brush to change its width\n", |
| 111 |
"- Drag the inside of the brush to translate it\n", |
| 112 |
"- Clicking and dragging outside of the brush deletes it and creates a new one." |
| 113 |
] |
| 114 |
}, |
| 115 |
{ |
| 116 |
"cell_type": "code", |
| 117 |
"execution_count": null, |
| 118 |
"metadata": {}, |
| 119 |
"outputs": [], |
| 120 |
"source": [ |
| 121 |
"# The Mark we want to select subsamples of\n", |
| 122 |
"scatter = Scatter(x=dates_actual, y=prices, scales=scales, colors=['orange'],\n", |
| 123 |
" selected_style={'opacity': '1'}, unselected_style={'opacity': '0.2'})\n", |
| 124 |
"# Create the brush selector, passing it its corresponding scale.\n", |
| 125 |
"# Notice that we do not pass it any marks for now\n", |
| 126 |
"brushintsel = BrushIntervalSelector(scale=scales['x'])\n", |
| 127 |
"\n", |
| 128 |
"x_ax = Axis(label='Index', scale=scales['x'])\n", |
| 129 |
"x_ay = Axis(label=(symbol + ' Price'), scale=scales['y'], orientation='vertical')\n", |
| 130 |
"# Pass the Selector instance to the Figure\n", |
| 131 |
"fig = Figure(marks=[scatter], axes=[x_ax, x_ay],\n", |
| 132 |
" title='''Brush Interval Selector Example. Click and drag on the Figure to action.''',\n", |
| 133 |
" interaction=brushintsel)\n", |
| 134 |
"\n", |
| 135 |
"# The following text widgets are used to display the `selected` attributes\n", |
| 136 |
"text_brush = HTML()\n", |
| 137 |
"text_scatter = HTML()\n", |
| 138 |
"\n", |
| 139 |
"# This function updates the text, triggered by a change in the selector\n", |
| 140 |
"def update_brush_text(*args):\n", |
| 141 |
" text_brush.value = \"The Brush's selected attribute is {}\".format(brushintsel.selected)\n", |
| 142 |
"def update_scatter_text(*args):\n", |
| 143 |
" text_scatter.value = \"The scatter's selected indices are {}\".format(scatter.selected)\n", |
| 144 |
"brushintsel.observe(update_brush_text, 'selected')\n", |
| 145 |
"scatter.observe(update_scatter_text, 'selected')\n", |
| 146 |
"\n", |
| 147 |
"update_brush_text()\n", |
| 148 |
"update_scatter_text()\n", |
| 149 |
"\n", |
| 150 |
"# Display\n", |
| 151 |
"VBox([fig, text_brush, text_scatter])" |
| 152 |
] |
| 153 |
}, |
| 154 |
{ |
| 155 |
"cell_type": "markdown", |
| 156 |
"metadata": {}, |
| 157 |
"source": [ |
| 158 |
"#### Linking the brush to the scatter\n", |
| 159 |
"Passing a mark (or several) to the selector, will link the mark's selected indices to the selector. " |
| 160 |
] |
| 161 |
}, |
| 162 |
{ |
| 163 |
"cell_type": "code", |
| 164 |
"execution_count": null, |
| 165 |
"metadata": {}, |
| 166 |
"outputs": [], |
| 167 |
"source": [ |
| 168 |
"brushintsel.marks = [scatter]" |
| 169 |
] |
| 170 |
}, |
| 171 |
{ |
| 172 |
"cell_type": "markdown", |
| 173 |
"metadata": {}, |
| 174 |
"source": [ |
| 175 |
"From now on we will stop printing out the selected indices, but rather use the `selected_style` and `unselected_style` attributes of the Marks to check which elements are selected." |
| 176 |
] |
| 177 |
}, |
| 178 |
{ |
| 179 |
"cell_type": "code", |
| 180 |
"execution_count": null, |
| 181 |
"metadata": {}, |
| 182 |
"outputs": [], |
| 183 |
"source": [ |
| 184 |
"def create_figure(selector, **selector_kwargs):\n", |
| 185 |
" '''\n", |
| 186 |
" Returns a Figure with a Scatter and a Selector.\n", |
| 187 |
" \n", |
| 188 |
" Arguments\n", |
| 189 |
" ---------\n", |
| 190 |
" selector: The type of Selector, one of\n", |
| 191 |
" {'BrushIntervalSelector', 'BrushSelector', 'FastIntervalSelector', 'IndexSelector', 'LassoSelector'}\n", |
| 192 |
" selector_kwargs: Arguments to be passed to the Selector\n", |
| 193 |
" '''\n", |
| 194 |
" scatter = Scatter(x=dates_actual, y=prices, scales=scales, colors=['orange'],\n", |
| 195 |
" selected_style={'opacity': '1'}, unselected_style={'opacity': '0.2'})\n", |
| 196 |
" sel = selector(marks=[scatter], **selector_kwargs)\n", |
| 197 |
" \n", |
| 198 |
" text_brush = HTML()\n", |
| 199 |
" if selector != LassoSelector:\n", |
| 200 |
" def update_text(*args):\n", |
| 201 |
" text_brush.value = '{}.selected = {}'.format(selector.__name__, sel.selected)\n", |
| 202 |
" sel.observe(update_text, 'selected')\n", |
| 203 |
" update_text()\n", |
| 204 |
"\n", |
| 205 |
" x_ax = Axis(label='Index', scale=scales['x'])\n", |
| 206 |
" x_ay = Axis(label=(symbol + ' Price'), scale=scales['y'], orientation='vertical')\n", |
| 207 |
" fig = Figure(marks=[scatter], axes=[x_ax, x_ay], title='{} Example'.format(selector.__name__),\n", |
| 208 |
" interaction=sel)\n", |
| 209 |
" return VBox([fig, text_brush])" |
| 210 |
] |
| 211 |
}, |
| 212 |
{ |
| 213 |
"cell_type": "markdown", |
| 214 |
"metadata": {}, |
| 215 |
"source": [ |
| 216 |
"### BrushIntervalSelector on the y-axis\n", |
| 217 |
"The attribute `orientation` can be set to 'vertical' to select on the y-axis. Be careful to pass the corresponding y-scale." |
| 218 |
] |
| 219 |
}, |
| 220 |
{ |
| 221 |
"cell_type": "code", |
| 222 |
"execution_count": null, |
| 223 |
"metadata": {}, |
| 224 |
"outputs": [], |
| 225 |
"source": [ |
| 226 |
"create_figure(BrushIntervalSelector, orientation='vertical', scale=scales['y'])" |
| 227 |
] |
| 228 |
}, |
| 229 |
{ |
| 230 |
"cell_type": "markdown", |
| 231 |
"metadata": {}, |
| 232 |
"source": [ |
| 233 |
"### 2d BrushSelector\n", |
| 234 |
"The `BrushSelector` is 2d, and must be fed 2 scales, `x_scale` and `y_scale`.\n", |
| 235 |
"\n", |
| 236 |
"Note that `BrushSelector.selected` is now 2x2. It is the coordinates of the lower left-hand and upper right-hand corners of the rectangle." |
| 237 |
] |
| 238 |
}, |
| 239 |
{ |
| 240 |
"cell_type": "code", |
| 241 |
"execution_count": null, |
| 242 |
"metadata": {}, |
| 243 |
"outputs": [], |
| 244 |
"source": [ |
| 245 |
"create_figure(BrushSelector, x_scale=scales['x'], y_scale=scales['y'])" |
| 246 |
] |
| 247 |
}, |
| 248 |
{ |
| 249 |
"cell_type": "markdown", |
| 250 |
"metadata": {}, |
| 251 |
"source": [ |
| 252 |
"FastIntervalSelector <a class=\"anchor\" id=\"fastintervalselector\"></a>\n", |
| 253 |
"--------------------\n", |
| 254 |
"\n", |
| 255 |
"The FastIntervalSelector is functionally like a BrushIntervalSelector, but provides a more fluid and rapid interaction.\n", |
| 256 |
"\n", |
| 257 |
"#### Usage:\n", |
| 258 |
"- The first click creates the selector.\n", |
| 259 |
"- Moving the mouse up and down widens and narrows the interval width.\n", |
| 260 |
"- Moving the mouse left and right translates the interval left and right.\n", |
| 261 |
"- Subsequent clicks will freeze/unfreeze the interval width\n", |
| 262 |
"- A double-click will freeze both the width and the translation\n", |
| 263 |
"\n", |
| 264 |
"Experiment and get a feel for it in the example below." |
| 265 |
] |
| 266 |
}, |
| 267 |
{ |
| 268 |
"cell_type": "code", |
| 269 |
"execution_count": null, |
| 270 |
"metadata": { |
| 271 |
"scrolled": false |
| 272 |
}, |
| 273 |
"outputs": [], |
| 274 |
"source": [ |
| 275 |
"create_figure(FastIntervalSelector, scale=scales['x'])" |
| 276 |
] |
| 277 |
}, |
| 278 |
{ |
| 279 |
"cell_type": "markdown", |
| 280 |
"metadata": {}, |
| 281 |
"source": [ |
| 282 |
"As of the latest version, `FastIntervalSelector` is only supported for 1d interaction along the x-axis" |
| 283 |
] |
| 284 |
}, |
| 285 |
{ |
| 286 |
"cell_type": "markdown", |
| 287 |
"metadata": {}, |
| 288 |
"source": [ |
| 289 |
"LassoSelector <a class=\"anchor\" id=\"lassoselector\"></a>\n", |
| 290 |
"--------------------\n", |
| 291 |
"\n", |
| 292 |
"This 2-D selector enables the user to select multiple sets of data points\n", |
| 293 |
"by drawing lassos on the figure. \n", |
| 294 |
"#### Usage:\n", |
| 295 |
"- Click and drag to draw a new lasso\n", |
| 296 |
"- Click a lasso to select (de-select) it. Mult\n", |
| 297 |
"- Press the 'Delete' button to delete the selected lassos" |
| 298 |
] |
| 299 |
}, |
| 300 |
{ |
| 301 |
"cell_type": "code", |
| 302 |
"execution_count": null, |
| 303 |
"metadata": {}, |
| 304 |
"outputs": [], |
| 305 |
"source": [ |
| 306 |
"create_figure(LassoSelector)" |
| 307 |
] |
| 308 |
}, |
| 309 |
{ |
| 310 |
"cell_type": "markdown", |
| 311 |
"metadata": {}, |
| 312 |
"source": [ |
| 313 |
"IndexSelector <a class=\"anchor\" id=\"indexselector\"></a>\n", |
| 314 |
"--------------------\n", |
| 315 |
"\n", |
| 316 |
"This 1-D selector selects a unique value on its scale. The attached Mark's selected element is the closest element to that value.\n", |
| 317 |
"#### Usage:\n", |
| 318 |
"- First click creates and activates the selector \n", |
| 319 |
"- Moving the mouse translates the selector\n", |
| 320 |
"- Subsequent clicks freeze/unfreeze the selector" |
| 321 |
] |
| 322 |
}, |
| 323 |
{ |
| 324 |
"cell_type": "code", |
| 325 |
"execution_count": null, |
| 326 |
"metadata": {}, |
| 327 |
"outputs": [], |
| 328 |
"source": [ |
| 329 |
"create_figure(IndexSelector, scale=scales['x'])" |
| 330 |
] |
| 331 |
}, |
| 332 |
{ |
| 333 |
"cell_type": "markdown", |
| 334 |
"metadata": { |
| 335 |
"collapsed": true |
| 336 |
}, |
| 337 |
"source": [ |
| 338 |
"As of the latest version, `IndexSelector` is only supported for interaction along the x-axis." |
| 339 |
] |
| 340 |
}, |
| 341 |
{ |
| 342 |
"cell_type": "markdown", |
| 343 |
"metadata": {}, |
| 344 |
"source": [ |
| 345 |
"MultiSelector <a class=\"anchor\" id=\"multiselector\"></a>\n", |
| 346 |
"--------------------\n", |
| 347 |
"\n", |
| 348 |
"This 1-D selector is equivalent to multiple brush selectors.\n", |
| 349 |
"#### Usage:\n", |
| 350 |
"* The first brush works like a regular brush.\n", |
| 351 |
"* `Ctrl + click` creates a new brush, which works like the regular brush. \n", |
| 352 |
"* The `active` brush has a Green border while all the `inactive` brushes have a Red border.\n", |
| 353 |
"* `Shift + click` deactivates the current `active` brush. Now, click on any `inactive` brush to make it `active`.\n", |
| 354 |
"* `Ctrl + Shift + click` clears and resets all the brushes.\n", |
| 355 |
"\n", |
| 356 |
"Each brush has a name (0, 1, 2, ... by default), and the `selected` attribute is a dict `{brush_name: brush_extent}`" |
| 357 |
] |
| 358 |
}, |
| 359 |
{ |
| 360 |
"cell_type": "code", |
| 361 |
"execution_count": null, |
| 362 |
"metadata": {}, |
| 363 |
"outputs": [], |
| 364 |
"source": [ |
| 365 |
"create_figure(MultiSelector, scale=scales['x'])" |
| 366 |
] |
| 367 |
} |
| 368 |
], |
| 369 |
"metadata": { |
| 370 |
"kernelspec": { |
| 371 |
"display_name": "Python 3", |
| 372 |
"language": "python", |
| 373 |
"name": "python3" |
| 374 |
}, |
| 375 |
"language_info": { |
| 376 |
"codemirror_mode": { |
| 377 |
"name": "ipython", |
| 378 |
"version": 3 |
| 379 |
}, |
| 380 |
"file_extension": ".py", |
| 381 |
"mimetype": "text/x-python", |
| 382 |
"name": "python", |
| 383 |
"nbconvert_exporter": "python", |
| 384 |
"pygments_lexer": "ipython3", |
| 385 |
"version": "3.7.6" |
| 386 |
} |
| 387 |
}, |
| 388 |
"nbformat": 4, |
| 389 |
"nbformat_minor": 2 |
| 390 |
} |