| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"metadata": {}, |
| 7 |
"outputs": [], |
| 8 |
"source": [ |
| 9 |
"import pandas as pd\n", |
| 10 |
"import numpy as np\n", |
| 11 |
"\n", |
| 12 |
"symbol = 'Security 1'\n", |
| 13 |
"symbol2 = 'Security 2'" |
| 14 |
] |
| 15 |
}, |
| 16 |
{ |
| 17 |
"cell_type": "code", |
| 18 |
"execution_count": null, |
| 19 |
"metadata": {}, |
| 20 |
"outputs": [], |
| 21 |
"source": [ |
| 22 |
"price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.4], [0.4, 1.0]]), axis=0) + 100,\n", |
| 23 |
" columns=['Security 1', 'Security 2'],\n", |
| 24 |
" index=pd.date_range(start='01-01-2007', periods=150))\n", |
| 25 |
"\n", |
| 26 |
"dates_actual = price_data.index.values\n", |
| 27 |
"prices = price_data[symbol].values" |
| 28 |
] |
| 29 |
}, |
| 30 |
{ |
| 31 |
"cell_type": "code", |
| 32 |
"execution_count": null, |
| 33 |
"metadata": {}, |
| 34 |
"outputs": [], |
| 35 |
"source": [ |
| 36 |
"from bqplot import DateScale, LinearScale, Axis, Lines, Scatter, Bars, Hist, Figure\n", |
| 37 |
"from bqplot.interacts import (\n", |
| 38 |
" FastIntervalSelector, IndexSelector, BrushIntervalSelector,\n", |
| 39 |
" BrushSelector, MultiSelector, LassoSelector, PanZoom, HandDraw\n", |
| 40 |
")\n", |
| 41 |
"from traitlets import link\n", |
| 42 |
"\n", |
| 43 |
"from ipywidgets import ToggleButtons, VBox, HTML" |
| 44 |
] |
| 45 |
}, |
| 46 |
{ |
| 47 |
"cell_type": "markdown", |
| 48 |
"metadata": {}, |
| 49 |
"source": [ |
| 50 |
"# Line Chart Selectors" |
| 51 |
] |
| 52 |
}, |
| 53 |
{ |
| 54 |
"cell_type": "markdown", |
| 55 |
"metadata": {}, |
| 56 |
"source": [ |
| 57 |
"## Fast Interval Selector" |
| 58 |
] |
| 59 |
}, |
| 60 |
{ |
| 61 |
"cell_type": "code", |
| 62 |
"execution_count": null, |
| 63 |
"metadata": {}, |
| 64 |
"outputs": [], |
| 65 |
"source": [ |
| 66 |
"## First we define a Figure\n", |
| 67 |
"dt_x_fast = DateScale()\n", |
| 68 |
"lin_y = LinearScale()\n", |
| 69 |
"\n", |
| 70 |
"x_ax = Axis(label='Index', scale=dt_x_fast)\n", |
| 71 |
"x_ay = Axis(label=(symbol + ' Price'), scale=lin_y, orientation='vertical')\n", |
| 72 |
"lc = Lines(x=dates_actual, y=prices, scales={'x': dt_x_fast, 'y': lin_y}, colors=['orange'])\n", |
| 73 |
"lc_2 = Lines(x=dates_actual[50:], y=prices[50:] + 2, scales={'x': dt_x_fast, 'y': lin_y}, colors=['blue'])" |
| 74 |
] |
| 75 |
}, |
| 76 |
{ |
| 77 |
"cell_type": "code", |
| 78 |
"execution_count": null, |
| 79 |
"metadata": {}, |
| 80 |
"outputs": [], |
| 81 |
"source": [ |
| 82 |
"## Next we define the type of selector we would like\n", |
| 83 |
"intsel_fast = FastIntervalSelector(scale=dt_x_fast, marks=[lc, lc_2])" |
| 84 |
] |
| 85 |
}, |
| 86 |
{ |
| 87 |
"cell_type": "code", |
| 88 |
"execution_count": null, |
| 89 |
"metadata": {}, |
| 90 |
"outputs": [], |
| 91 |
"source": [ |
| 92 |
"## Now, we define a function that will be called when the FastIntervalSelector is interacted with\n", |
| 93 |
"def fast_interval_change_callback(change):\n", |
| 94 |
" db_fast.value = 'The selected period is ' + str(change.new)" |
| 95 |
] |
| 96 |
}, |
| 97 |
{ |
| 98 |
"cell_type": "code", |
| 99 |
"execution_count": null, |
| 100 |
"metadata": {}, |
| 101 |
"outputs": [], |
| 102 |
"source": [ |
| 103 |
"## Now we connect the selectors to that function\n", |
| 104 |
"intsel_fast.observe(fast_interval_change_callback, names=['selected'])" |
| 105 |
] |
| 106 |
}, |
| 107 |
{ |
| 108 |
"cell_type": "code", |
| 109 |
"execution_count": null, |
| 110 |
"metadata": {}, |
| 111 |
"outputs": [], |
| 112 |
"source": [ |
| 113 |
"## We use the HTML widget to see the value of what we are selecting and modify it when an interaction is performed\n", |
| 114 |
"## on the selector\n", |
| 115 |
"db_fast = HTML()\n", |
| 116 |
"db_fast.value = 'The selected period is ' + str(intsel_fast.selected)\n", |
| 117 |
"\n", |
| 118 |
"fig_fast_intsel = Figure(marks=[lc, lc_2], axes=[x_ax, x_ay], title='Fast Interval Selector Example',\n", |
| 119 |
" interaction=intsel_fast) #This is where we assign the interaction to this particular Figure\n", |
| 120 |
"\n", |
| 121 |
"VBox([db_fast, fig_fast_intsel])" |
| 122 |
] |
| 123 |
}, |
| 124 |
{ |
| 125 |
"cell_type": "markdown", |
| 126 |
"metadata": {}, |
| 127 |
"source": [ |
| 128 |
"## Index Selector" |
| 129 |
] |
| 130 |
}, |
| 131 |
{ |
| 132 |
"cell_type": "code", |
| 133 |
"execution_count": null, |
| 134 |
"metadata": {}, |
| 135 |
"outputs": [], |
| 136 |
"source": [ |
| 137 |
"db_index = HTML(value='[]')" |
| 138 |
] |
| 139 |
}, |
| 140 |
{ |
| 141 |
"cell_type": "code", |
| 142 |
"execution_count": null, |
| 143 |
"metadata": {}, |
| 144 |
"outputs": [], |
| 145 |
"source": [ |
| 146 |
"## Now we try a selector made to select all the y-values associated with a single x-value\n", |
| 147 |
"index_sel = IndexSelector(scale=dt_x_fast, marks=[lc, lc_2])" |
| 148 |
] |
| 149 |
}, |
| 150 |
{ |
| 151 |
"cell_type": "code", |
| 152 |
"execution_count": null, |
| 153 |
"metadata": {}, |
| 154 |
"outputs": [], |
| 155 |
"source": [ |
| 156 |
"## Now, we define a function that will be called when the selectors are interacted with\n", |
| 157 |
"def index_change_callback(change):\n", |
| 158 |
" db_index.value = 'The selected date is ' + str(change.new)" |
| 159 |
] |
| 160 |
}, |
| 161 |
{ |
| 162 |
"cell_type": "code", |
| 163 |
"execution_count": null, |
| 164 |
"metadata": {}, |
| 165 |
"outputs": [], |
| 166 |
"source": [ |
| 167 |
"index_sel.observe(index_change_callback, names=['selected'])" |
| 168 |
] |
| 169 |
}, |
| 170 |
{ |
| 171 |
"cell_type": "code", |
| 172 |
"execution_count": null, |
| 173 |
"metadata": {}, |
| 174 |
"outputs": [], |
| 175 |
"source": [ |
| 176 |
"fig_index_sel = Figure(marks=[lc, lc_2], axes=[x_ax, x_ay], title='Index Selector Example',\n", |
| 177 |
" interaction=index_sel)\n", |
| 178 |
"VBox([db_index, fig_index_sel])" |
| 179 |
] |
| 180 |
}, |
| 181 |
{ |
| 182 |
"cell_type": "markdown", |
| 183 |
"metadata": {}, |
| 184 |
"source": [ |
| 185 |
"## Returning indexes of selected values" |
| 186 |
] |
| 187 |
}, |
| 188 |
{ |
| 189 |
"cell_type": "code", |
| 190 |
"execution_count": null, |
| 191 |
"metadata": {}, |
| 192 |
"outputs": [], |
| 193 |
"source": [ |
| 194 |
"from datetime import datetime as py_dtime\n", |
| 195 |
"\n", |
| 196 |
"dt_x_index = DateScale(min=np.datetime64(py_dtime(2006, 6, 1)))\n", |
| 197 |
"lin_y2 = LinearScale()\n", |
| 198 |
"\n", |
| 199 |
"lc2_index = Lines(x=dates_actual, y=prices,\n", |
| 200 |
" scales={'x': dt_x_index, 'y': lin_y2})\n", |
| 201 |
"\n", |
| 202 |
"x_ax1 = Axis(label='Date', scale=dt_x_index)\n", |
| 203 |
"x_ay2 = Axis(label=(symbol + ' Price'), scale=lin_y2, orientation='vertical')" |
| 204 |
] |
| 205 |
}, |
| 206 |
{ |
| 207 |
"cell_type": "code", |
| 208 |
"execution_count": null, |
| 209 |
"metadata": {}, |
| 210 |
"outputs": [], |
| 211 |
"source": [ |
| 212 |
"intsel_date = FastIntervalSelector(scale=dt_x_index, marks=[lc2_index])" |
| 213 |
] |
| 214 |
}, |
| 215 |
{ |
| 216 |
"cell_type": "code", |
| 217 |
"execution_count": null, |
| 218 |
"metadata": {}, |
| 219 |
"outputs": [], |
| 220 |
"source": [ |
| 221 |
"db_date = HTML()\n", |
| 222 |
"db_date.value = str(intsel_date.selected)" |
| 223 |
] |
| 224 |
}, |
| 225 |
{ |
| 226 |
"cell_type": "code", |
| 227 |
"execution_count": null, |
| 228 |
"metadata": {}, |
| 229 |
"outputs": [], |
| 230 |
"source": [ |
| 231 |
"## Now, we define a function that will be called when the selectors are interacted with - a callback\n", |
| 232 |
"def date_interval_change_callback(change):\n", |
| 233 |
" db_date.value = str(change.new)" |
| 234 |
] |
| 235 |
}, |
| 236 |
{ |
| 237 |
"cell_type": "code", |
| 238 |
"execution_count": null, |
| 239 |
"metadata": {}, |
| 240 |
"outputs": [], |
| 241 |
"source": [ |
| 242 |
"## Notice here that we call the observe on the Mark lc2_index rather than on the selector intsel_date\n", |
| 243 |
"lc2_index.observe(date_interval_change_callback, names=['selected'])\n", |
| 244 |
"\n", |
| 245 |
"fig_date_mark = Figure(marks=[lc2_index], axes=[x_ax1, x_ay2],\n", |
| 246 |
" title='Fast Interval Selector Selected Indices Example', interaction=intsel_date)\n", |
| 247 |
"\n", |
| 248 |
"VBox([db_date, fig_date_mark])" |
| 249 |
] |
| 250 |
}, |
| 251 |
{ |
| 252 |
"cell_type": "markdown", |
| 253 |
"metadata": {}, |
| 254 |
"source": [ |
| 255 |
"## Brush Selector" |
| 256 |
] |
| 257 |
}, |
| 258 |
{ |
| 259 |
"cell_type": "markdown", |
| 260 |
"metadata": {}, |
| 261 |
"source": [ |
| 262 |
"### We can do the same with any type of selector" |
| 263 |
] |
| 264 |
}, |
| 265 |
{ |
| 266 |
"cell_type": "code", |
| 267 |
"execution_count": null, |
| 268 |
"metadata": {}, |
| 269 |
"outputs": [], |
| 270 |
"source": [ |
| 271 |
"## Defining a new Figure\n", |
| 272 |
"dt_x_brush = DateScale(min=np.datetime64(py_dtime(2006, 6, 1)))\n", |
| 273 |
"lin_y2_brush = LinearScale()\n", |
| 274 |
"\n", |
| 275 |
"lc3_brush = Lines(x=dates_actual, y=prices,\n", |
| 276 |
" scales={'x': dt_x_brush, 'y': lin_y2_brush})\n", |
| 277 |
"\n", |
| 278 |
"x_ax_brush = Axis(label='Date', scale=dt_x_brush)\n", |
| 279 |
"x_ay_brush = Axis(label=(symbol + ' Price'), scale=lin_y2_brush, orientation='vertical')" |
| 280 |
] |
| 281 |
}, |
| 282 |
{ |
| 283 |
"cell_type": "code", |
| 284 |
"execution_count": null, |
| 285 |
"metadata": {}, |
| 286 |
"outputs": [], |
| 287 |
"source": [ |
| 288 |
"db_brush = HTML(value='[]')" |
| 289 |
] |
| 290 |
}, |
| 291 |
{ |
| 292 |
"cell_type": "code", |
| 293 |
"execution_count": null, |
| 294 |
"metadata": {}, |
| 295 |
"outputs": [], |
| 296 |
"source": [ |
| 297 |
"brushsel_date = BrushIntervalSelector(scale=dt_x_brush, marks=[lc3_brush], color='FireBrick')" |
| 298 |
] |
| 299 |
}, |
| 300 |
{ |
| 301 |
"cell_type": "code", |
| 302 |
"execution_count": null, |
| 303 |
"metadata": {}, |
| 304 |
"outputs": [], |
| 305 |
"source": [ |
| 306 |
"## Now, we define a function that will be called when the selectors are interacted with - a callback\n", |
| 307 |
"def date_brush_change_callback(change):\n", |
| 308 |
" db_brush.value = str(change.new)" |
| 309 |
] |
| 310 |
}, |
| 311 |
{ |
| 312 |
"cell_type": "code", |
| 313 |
"execution_count": null, |
| 314 |
"metadata": {}, |
| 315 |
"outputs": [], |
| 316 |
"source": [ |
| 317 |
"lc3_brush.observe(date_brush_change_callback, names=['selected'])" |
| 318 |
] |
| 319 |
}, |
| 320 |
{ |
| 321 |
"cell_type": "code", |
| 322 |
"execution_count": null, |
| 323 |
"metadata": {}, |
| 324 |
"outputs": [], |
| 325 |
"source": [ |
| 326 |
"fig_brush_sel = Figure(marks=[lc3_brush], axes=[x_ax_brush, x_ay_brush],\n", |
| 327 |
" title='Brush Selector Selected Indices Example', interaction=brushsel_date)\n", |
| 328 |
"\n", |
| 329 |
"VBox([db_brush, fig_brush_sel])" |
| 330 |
] |
| 331 |
}, |
| 332 |
{ |
| 333 |
"cell_type": "markdown", |
| 334 |
"metadata": {}, |
| 335 |
"source": [ |
| 336 |
"# Scatter Chart Selectors" |
| 337 |
] |
| 338 |
}, |
| 339 |
{ |
| 340 |
"cell_type": "markdown", |
| 341 |
"metadata": {}, |
| 342 |
"source": [ |
| 343 |
"## Brush Selector" |
| 344 |
] |
| 345 |
}, |
| 346 |
{ |
| 347 |
"cell_type": "code", |
| 348 |
"execution_count": null, |
| 349 |
"metadata": {}, |
| 350 |
"outputs": [], |
| 351 |
"source": [ |
| 352 |
"date_fmt = '%m-%d-%Y'\n", |
| 353 |
"\n", |
| 354 |
"sec2_data = price_data[symbol2].values\n", |
| 355 |
"dates = price_data.index.values" |
| 356 |
] |
| 357 |
}, |
| 358 |
{ |
| 359 |
"cell_type": "code", |
| 360 |
"execution_count": null, |
| 361 |
"metadata": {}, |
| 362 |
"outputs": [], |
| 363 |
"source": [ |
| 364 |
"sc_x = LinearScale()\n", |
| 365 |
"sc_y = LinearScale()\n", |
| 366 |
"\n", |
| 367 |
"scatt = Scatter(x=prices, y=sec2_data,\n", |
| 368 |
" scales={'x': sc_x, 'y': sc_y})\n", |
| 369 |
"\n", |
| 370 |
"sc_xax = Axis(label=(symbol), scale=sc_x)\n", |
| 371 |
"sc_yax = Axis(label=(symbol2), scale=sc_y, orientation='vertical')" |
| 372 |
] |
| 373 |
}, |
| 374 |
{ |
| 375 |
"cell_type": "code", |
| 376 |
"execution_count": null, |
| 377 |
"metadata": {}, |
| 378 |
"outputs": [], |
| 379 |
"source": [ |
| 380 |
"br_sel = BrushSelector(x_scale=sc_x, y_scale=sc_y, marks=[scatt], color='red')\n", |
| 381 |
"\n", |
| 382 |
"db_scat_brush = HTML(value='[]')" |
| 383 |
] |
| 384 |
}, |
| 385 |
{ |
| 386 |
"cell_type": "code", |
| 387 |
"execution_count": null, |
| 388 |
"metadata": {}, |
| 389 |
"outputs": [], |
| 390 |
"source": [ |
| 391 |
"## call back for the selector\n", |
| 392 |
"def brush_callback(change):\n", |
| 393 |
" db_scat_brush.value = str(br_sel.selected)" |
| 394 |
] |
| 395 |
}, |
| 396 |
{ |
| 397 |
"cell_type": "code", |
| 398 |
"execution_count": null, |
| 399 |
"metadata": {}, |
| 400 |
"outputs": [], |
| 401 |
"source": [ |
| 402 |
"br_sel.observe(brush_callback, names=['brushing'])" |
| 403 |
] |
| 404 |
}, |
| 405 |
{ |
| 406 |
"cell_type": "code", |
| 407 |
"execution_count": null, |
| 408 |
"metadata": {}, |
| 409 |
"outputs": [], |
| 410 |
"source": [ |
| 411 |
"fig_scat_brush = Figure(marks=[scatt], axes=[sc_xax, sc_yax], title='Scatter Chart Brush Selector Example',\n", |
| 412 |
" interaction=br_sel)" |
| 413 |
] |
| 414 |
}, |
| 415 |
{ |
| 416 |
"cell_type": "code", |
| 417 |
"execution_count": null, |
| 418 |
"metadata": { |
| 419 |
"scrolled": false |
| 420 |
}, |
| 421 |
"outputs": [], |
| 422 |
"source": [ |
| 423 |
"VBox([db_scat_brush, fig_scat_brush])" |
| 424 |
] |
| 425 |
}, |
| 426 |
{ |
| 427 |
"cell_type": "markdown", |
| 428 |
"metadata": {}, |
| 429 |
"source": [ |
| 430 |
"## Brush Selector with Date Values" |
| 431 |
] |
| 432 |
}, |
| 433 |
{ |
| 434 |
"cell_type": "code", |
| 435 |
"execution_count": null, |
| 436 |
"metadata": {}, |
| 437 |
"outputs": [], |
| 438 |
"source": [ |
| 439 |
"sc_brush_dt_x = DateScale(date_format=date_fmt)\n", |
| 440 |
"sc_brush_dt_y = LinearScale()\n", |
| 441 |
"\n", |
| 442 |
"scatt2 = Scatter(x=dates_actual, y=sec2_data,\n", |
| 443 |
" scales={'x': sc_brush_dt_x, 'y': sc_brush_dt_y})" |
| 444 |
] |
| 445 |
}, |
| 446 |
{ |
| 447 |
"cell_type": "code", |
| 448 |
"execution_count": null, |
| 449 |
"metadata": {}, |
| 450 |
"outputs": [], |
| 451 |
"source": [ |
| 452 |
"br_sel_dt = BrushSelector(x_scale=sc_brush_dt_x, y_scale=sc_brush_dt_y, marks=[scatt2])" |
| 453 |
] |
| 454 |
}, |
| 455 |
{ |
| 456 |
"cell_type": "code", |
| 457 |
"execution_count": null, |
| 458 |
"metadata": {}, |
| 459 |
"outputs": [], |
| 460 |
"source": [ |
| 461 |
"db_brush_dt = HTML(value=str(br_sel_dt.selected))" |
| 462 |
] |
| 463 |
}, |
| 464 |
{ |
| 465 |
"cell_type": "code", |
| 466 |
"execution_count": null, |
| 467 |
"metadata": {}, |
| 468 |
"outputs": [], |
| 469 |
"source": [ |
| 470 |
"## call back for the selector\n", |
| 471 |
"def brush_dt_callback(change):\n", |
| 472 |
" db_brush_dt.value = str(br_sel_dt.selected)" |
| 473 |
] |
| 474 |
}, |
| 475 |
{ |
| 476 |
"cell_type": "code", |
| 477 |
"execution_count": null, |
| 478 |
"metadata": {}, |
| 479 |
"outputs": [], |
| 480 |
"source": [ |
| 481 |
"br_sel_dt.observe(brush_dt_callback, names=['brushing'])" |
| 482 |
] |
| 483 |
}, |
| 484 |
{ |
| 485 |
"cell_type": "code", |
| 486 |
"execution_count": null, |
| 487 |
"metadata": {}, |
| 488 |
"outputs": [], |
| 489 |
"source": [ |
| 490 |
"sc_xax = Axis(label=(symbol), scale=sc_brush_dt_x)\n", |
| 491 |
"sc_yax = Axis(label=(symbol2), scale=sc_brush_dt_y, orientation='vertical')\n", |
| 492 |
"fig_brush_dt = Figure(marks =[scatt2], axes=[sc_xax, sc_yax], title='Brush Selector with Dates Example',\n", |
| 493 |
" interaction=br_sel_dt)" |
| 494 |
] |
| 495 |
}, |
| 496 |
{ |
| 497 |
"cell_type": "code", |
| 498 |
"execution_count": null, |
| 499 |
"metadata": {}, |
| 500 |
"outputs": [], |
| 501 |
"source": [ |
| 502 |
"VBox([db_brush_dt, fig_brush_dt])" |
| 503 |
] |
| 504 |
}, |
| 505 |
{ |
| 506 |
"cell_type": "markdown", |
| 507 |
"metadata": {}, |
| 508 |
"source": [ |
| 509 |
"# Histogram Selectors" |
| 510 |
] |
| 511 |
}, |
| 512 |
{ |
| 513 |
"cell_type": "code", |
| 514 |
"execution_count": null, |
| 515 |
"metadata": {}, |
| 516 |
"outputs": [], |
| 517 |
"source": [ |
| 518 |
"## call back for selectors\n", |
| 519 |
"def interval_change_callback(name, value):\n", |
| 520 |
" db3.value = str(value)\n", |
| 521 |
" \n", |
| 522 |
"## call back for the selector\n", |
| 523 |
"def brush_callback(change):\n", |
| 524 |
" if(not br_intsel.brushing):\n", |
| 525 |
" db3.value = str(br_intsel.selected)" |
| 526 |
] |
| 527 |
}, |
| 528 |
{ |
| 529 |
"cell_type": "code", |
| 530 |
"execution_count": null, |
| 531 |
"metadata": {}, |
| 532 |
"outputs": [], |
| 533 |
"source": [ |
| 534 |
"returns = np.log(prices[1:]) - np.log(prices[:-1])\n", |
| 535 |
"hist_x = LinearScale()\n", |
| 536 |
"hist_y = LinearScale()\n", |
| 537 |
"hist = Hist(sample=returns, scales={'sample': hist_x, 'count': hist_y})\n", |
| 538 |
"\n", |
| 539 |
"br_intsel = BrushIntervalSelector(scale=hist_x, marks=[hist])\n", |
| 540 |
"br_intsel.observe(brush_callback, names=['selected'])\n", |
| 541 |
"br_intsel.observe(brush_callback, names=['brushing'])\n", |
| 542 |
"\n", |
| 543 |
"db3 = HTML()\n", |
| 544 |
"db3.value = str(br_intsel.selected)\n", |
| 545 |
"\n", |
| 546 |
"h_xax = Axis(scale=hist_x, label='Returns', grids='off', set_ticks=True, tick_format='0.2%')\n", |
| 547 |
"h_yax = Axis(scale=hist_y, label='Freq', orientation='vertical', grid_lines='none')\n", |
| 548 |
"\n", |
| 549 |
"fig_hist = Figure(marks=[hist], axes=[h_xax, h_yax], title='Histogram Selection Example', interaction=br_intsel)\n", |
| 550 |
"VBox([db3, fig_hist])" |
| 551 |
] |
| 552 |
}, |
| 553 |
{ |
| 554 |
"cell_type": "markdown", |
| 555 |
"metadata": {}, |
| 556 |
"source": [ |
| 557 |
"## Multi Selector\n", |
| 558 |
"\n", |
| 559 |
"* This selector provides the ability to have multiple brush selectors on the same graph.\n", |
| 560 |
"* The first brush works like a regular brush.\n", |
| 561 |
"* `Ctrl + click` creates a new brush, which works like the regular brush. \n", |
| 562 |
"* The `active` brush has a Green border while all the `inactive` brushes have a Red border.\n", |
| 563 |
"* `Shift + click` deactivates the current `active` brush. Now, click on any `inactive` brush to make it `active`.\n", |
| 564 |
"* `Ctrl + Alt + Shift + click` clears and resets all the brushes." |
| 565 |
] |
| 566 |
}, |
| 567 |
{ |
| 568 |
"cell_type": "code", |
| 569 |
"execution_count": null, |
| 570 |
"metadata": {}, |
| 571 |
"outputs": [], |
| 572 |
"source": [ |
| 573 |
"def multi_sel_callback(change):\n", |
| 574 |
" if(not multi_sel.brushing):\n", |
| 575 |
" db4.value = str(multi_sel.selected)" |
| 576 |
] |
| 577 |
}, |
| 578 |
{ |
| 579 |
"cell_type": "code", |
| 580 |
"execution_count": null, |
| 581 |
"metadata": {}, |
| 582 |
"outputs": [], |
| 583 |
"source": [ |
| 584 |
"line_x = LinearScale()\n", |
| 585 |
"line_y = LinearScale()\n", |
| 586 |
"line = Lines(x=np.arange(100), y=np.random.randn(100), scales={'x': line_x, 'y': line_y})\n", |
| 587 |
"\n", |
| 588 |
"multi_sel = MultiSelector(scale=line_x, marks=[line])\n", |
| 589 |
"multi_sel.observe(multi_sel_callback, names=['selected'])\n", |
| 590 |
"multi_sel.observe(multi_sel_callback, names=['brushing'])\n", |
| 591 |
"\n", |
| 592 |
"db4 = HTML()\n", |
| 593 |
"db4.value = str(multi_sel.selected)\n", |
| 594 |
"\n", |
| 595 |
"h_xax = Axis(scale=line_x, label='Returns', grid_lines='none')\n", |
| 596 |
"h_yax = Axis(scale=hist_y, label='Freq', orientation='vertical', grid_lines='none')\n", |
| 597 |
"\n", |
| 598 |
"fig_multi = Figure(marks=[line], axes=[h_xax, h_yax], title='Multi-Selector Example',\n", |
| 599 |
" interaction=multi_sel)\n", |
| 600 |
"VBox([db4, fig_multi])" |
| 601 |
] |
| 602 |
}, |
| 603 |
{ |
| 604 |
"cell_type": "code", |
| 605 |
"execution_count": null, |
| 606 |
"metadata": {}, |
| 607 |
"outputs": [], |
| 608 |
"source": [ |
| 609 |
"# changing the names of the intervals.\n", |
| 610 |
"multi_sel.names = ['int1', 'int2', 'int3']" |
| 611 |
] |
| 612 |
}, |
| 613 |
{ |
| 614 |
"cell_type": "markdown", |
| 615 |
"metadata": {}, |
| 616 |
"source": [ |
| 617 |
"## Multi Selector with Date X" |
| 618 |
] |
| 619 |
}, |
| 620 |
{ |
| 621 |
"cell_type": "code", |
| 622 |
"execution_count": null, |
| 623 |
"metadata": {}, |
| 624 |
"outputs": [], |
| 625 |
"source": [ |
| 626 |
"def multi_sel_dt_callback(change):\n", |
| 627 |
" if(not multi_sel_dt.brushing):\n", |
| 628 |
" db_multi_dt.value = str(multi_sel_dt.selected)" |
| 629 |
] |
| 630 |
}, |
| 631 |
{ |
| 632 |
"cell_type": "code", |
| 633 |
"execution_count": null, |
| 634 |
"metadata": {}, |
| 635 |
"outputs": [], |
| 636 |
"source": [ |
| 637 |
"line_dt_x = DateScale(min=np.datetime64(py_dtime(2007, 1, 1)))\n", |
| 638 |
"line_dt_y = LinearScale()\n", |
| 639 |
"line_dt = Lines(x=dates_actual, y=sec2_data, scales={'x': line_dt_x, 'y': line_dt_y}, colors=['red'])\n", |
| 640 |
"\n", |
| 641 |
"multi_sel_dt = MultiSelector(scale=line_dt_x)\n", |
| 642 |
"multi_sel_dt.observe(multi_sel_dt_callback, names=['selected'])\n", |
| 643 |
"multi_sel_dt.observe(multi_sel_dt_callback, names=['brushing'])\n", |
| 644 |
"\n", |
| 645 |
"db_multi_dt = HTML()\n", |
| 646 |
"db_multi_dt.value = str(multi_sel_dt.selected)\n", |
| 647 |
"\n", |
| 648 |
"h_xax_dt = Axis(scale=line_dt_x, label='Returns', grid_lines='none')\n", |
| 649 |
"h_yax_dt = Axis(scale=line_dt_y, label='Freq', orientation='vertical', grid_lines='none')\n", |
| 650 |
"\n", |
| 651 |
"fig_multi_dt = Figure(marks=[line_dt], axes=[h_xax_dt, h_yax_dt], title='Multi-Selector with Date Example',\n", |
| 652 |
" interaction=multi_sel_dt)\n", |
| 653 |
"VBox([db_multi_dt, fig_multi_dt])" |
| 654 |
] |
| 655 |
}, |
| 656 |
{ |
| 657 |
"cell_type": "markdown", |
| 658 |
"metadata": {}, |
| 659 |
"source": [ |
| 660 |
"## Lasso Selector" |
| 661 |
] |
| 662 |
}, |
| 663 |
{ |
| 664 |
"cell_type": "code", |
| 665 |
"execution_count": null, |
| 666 |
"metadata": {}, |
| 667 |
"outputs": [], |
| 668 |
"source": [ |
| 669 |
"lasso_sel = LassoSelector()" |
| 670 |
] |
| 671 |
}, |
| 672 |
{ |
| 673 |
"cell_type": "code", |
| 674 |
"execution_count": null, |
| 675 |
"metadata": {}, |
| 676 |
"outputs": [], |
| 677 |
"source": [ |
| 678 |
"xs, ys = LinearScale(), LinearScale()\n", |
| 679 |
"data = np.arange(20)\n", |
| 680 |
"line_lasso = Lines(x=data, y=data, scales={'x': xs, 'y': ys})\n", |
| 681 |
"scatter_lasso = Scatter(x=data, y=data, scales={'x': xs, 'y': ys}, colors=['skyblue'])\n", |
| 682 |
"bar_lasso = Bars(x=data, y=data/2., scales={'x': xs, 'y': ys})\n", |
| 683 |
"xax_lasso, yax_lasso = Axis(scale=xs, label='X'), Axis(scale=ys, label='Y', orientation='vertical')\n", |
| 684 |
"fig_lasso = Figure(marks=[scatter_lasso, line_lasso, bar_lasso], axes=[xax_lasso, yax_lasso],\n", |
| 685 |
" title='Lasso Selector Example', interaction=lasso_sel)\n", |
| 686 |
"lasso_sel.marks = [scatter_lasso, line_lasso]\n", |
| 687 |
"fig_lasso" |
| 688 |
] |
| 689 |
}, |
| 690 |
{ |
| 691 |
"cell_type": "code", |
| 692 |
"execution_count": null, |
| 693 |
"metadata": {}, |
| 694 |
"outputs": [], |
| 695 |
"source": [ |
| 696 |
"scatter_lasso.selected, line_lasso.selected" |
| 697 |
] |
| 698 |
}, |
| 699 |
{ |
| 700 |
"cell_type": "markdown", |
| 701 |
"metadata": {}, |
| 702 |
"source": [ |
| 703 |
"## Pan Zoom" |
| 704 |
] |
| 705 |
}, |
| 706 |
{ |
| 707 |
"cell_type": "code", |
| 708 |
"execution_count": null, |
| 709 |
"metadata": {}, |
| 710 |
"outputs": [], |
| 711 |
"source": [ |
| 712 |
"xs_pz = DateScale(min=np.datetime64(py_dtime(2007, 1, 1)))\n", |
| 713 |
"ys_pz = LinearScale()\n", |
| 714 |
"line_pz = Lines(x=dates_actual, y=sec2_data, scales={'x': xs_pz, 'y': ys_pz}, colors=['red'])\n", |
| 715 |
"\n", |
| 716 |
"panzoom = PanZoom(scales={'x': [xs_pz], 'y': [ys_pz]})\n", |
| 717 |
"xax = Axis(scale=xs_pz, label='Date', grids='off')\n", |
| 718 |
"yax = Axis(scale=ys_pz, label='Price', orientation='vertical', grid_lines='none')\n", |
| 719 |
"\n", |
| 720 |
"Figure(marks=[line_pz], axes=[xax, yax], interaction=panzoom)" |
| 721 |
] |
| 722 |
}, |
| 723 |
{ |
| 724 |
"cell_type": "markdown", |
| 725 |
"metadata": {}, |
| 726 |
"source": [ |
| 727 |
"## Hand Draw" |
| 728 |
] |
| 729 |
}, |
| 730 |
{ |
| 731 |
"cell_type": "code", |
| 732 |
"execution_count": null, |
| 733 |
"metadata": {}, |
| 734 |
"outputs": [], |
| 735 |
"source": [ |
| 736 |
"xs_hd = DateScale(min=np.datetime64(py_dtime(2007, 1, 1)))\n", |
| 737 |
"ys_hd = LinearScale()\n", |
| 738 |
"line_hd = Lines(x=dates_actual, y=sec2_data, scales={'x': xs_hd, 'y': ys_hd}, colors=['red'])\n", |
| 739 |
"\n", |
| 740 |
"handdraw = HandDraw(lines=line_hd)\n", |
| 741 |
"xax = Axis(scale=xs_hd, label='Date', grid_lines='none')\n", |
| 742 |
"yax = Axis(scale=ys_hd, label='Price', orientation='vertical', grid_lines='none')\n", |
| 743 |
"\n", |
| 744 |
"Figure(marks=[line_hd], axes=[xax, yax], interaction=handdraw)" |
| 745 |
] |
| 746 |
}, |
| 747 |
{ |
| 748 |
"cell_type": "markdown", |
| 749 |
"metadata": {}, |
| 750 |
"source": [ |
| 751 |
"# Unified Figure with All Interactions" |
| 752 |
] |
| 753 |
}, |
| 754 |
{ |
| 755 |
"cell_type": "code", |
| 756 |
"execution_count": null, |
| 757 |
"metadata": {}, |
| 758 |
"outputs": [], |
| 759 |
"source": [ |
| 760 |
"dt_x = DateScale(date_format=date_fmt, min=py_dtime(2007, 1, 1))\n", |
| 761 |
"lc1_x = LinearScale()\n", |
| 762 |
"lc2_y = LinearScale()\n", |
| 763 |
"\n", |
| 764 |
"lc2 = Lines(x=np.linspace(0.0, 10.0, len(prices)), y=prices * 0.25,\n", |
| 765 |
" scales={'x': lc1_x, 'y': lc2_y}, \n", |
| 766 |
" display_legend=True,\n", |
| 767 |
" labels=['Security 1'])\n", |
| 768 |
"\n", |
| 769 |
"lc3 = Lines(x=dates_actual, y=sec2_data,\n", |
| 770 |
" scales={'x': dt_x, 'y': lc2_y},\n", |
| 771 |
" colors=['red'], \n", |
| 772 |
" display_legend=True, \n", |
| 773 |
" labels=['Security 2'])\n", |
| 774 |
"\n", |
| 775 |
"lc4 = Lines(x=np.linspace(0.0, 10.0, len(prices)), y=sec2_data * 0.75,\n", |
| 776 |
" scales={'x': LinearScale(min=5, max=10), 'y': lc2_y},\n", |
| 777 |
" colors=['green'], display_legend=True, \n", |
| 778 |
" labels=['Security 2 squared'])\n", |
| 779 |
"\n", |
| 780 |
"x_ax1 = Axis(label='Date', scale=dt_x)\n", |
| 781 |
"x_ax2 = Axis(label='Time', scale=lc1_x, side='top', grid_lines='none')\n", |
| 782 |
"x_ay2 = Axis(label=(symbol + ' Price'), scale=lc2_y, orientation='vertical')\n", |
| 783 |
"\n", |
| 784 |
"\n", |
| 785 |
"fig = Figure(marks=[lc2, lc3, lc4], axes=[x_ax1, x_ax2, x_ay2])" |
| 786 |
] |
| 787 |
}, |
| 788 |
{ |
| 789 |
"cell_type": "code", |
| 790 |
"execution_count": null, |
| 791 |
"metadata": {}, |
| 792 |
"outputs": [], |
| 793 |
"source": [ |
| 794 |
"## declaring the interactions\n", |
| 795 |
"multi_sel = MultiSelector(scale=dt_x, marks=[lc2, lc3])\n", |
| 796 |
"br_intsel = BrushIntervalSelector(scale=lc1_x, marks=[lc2, lc3])\n", |
| 797 |
"index_sel = IndexSelector(scale=dt_x, marks=[lc2, lc3])\n", |
| 798 |
"int_sel = FastIntervalSelector(scale=dt_x, marks=[lc3, lc2])\n", |
| 799 |
"\n", |
| 800 |
"hd = HandDraw(lines=lc2)\n", |
| 801 |
"hd2 = HandDraw(lines=lc3)\n", |
| 802 |
"pz = PanZoom(scales={'x': [dt_x], 'y': [lc2_y]})\n", |
| 803 |
"\n", |
| 804 |
"deb = HTML()\n", |
| 805 |
"deb.value = '[]'" |
| 806 |
] |
| 807 |
}, |
| 808 |
{ |
| 809 |
"cell_type": "code", |
| 810 |
"execution_count": null, |
| 811 |
"metadata": {}, |
| 812 |
"outputs": [], |
| 813 |
"source": [ |
| 814 |
"## Call back handler for the interactions\n", |
| 815 |
"def test_callback(change):\n", |
| 816 |
" deb.value = str(change.new)\n", |
| 817 |
" \n", |
| 818 |
"multi_sel.observe(test_callback, names=['selected'])\n", |
| 819 |
"br_intsel.observe(test_callback, names=['selected'])\n", |
| 820 |
"index_sel.observe(test_callback, names=['selected'])\n", |
| 821 |
"int_sel.observe(test_callback, names=['selected'])" |
| 822 |
] |
| 823 |
}, |
| 824 |
{ |
| 825 |
"cell_type": "code", |
| 826 |
"execution_count": null, |
| 827 |
"metadata": {}, |
| 828 |
"outputs": [], |
| 829 |
"source": [ |
| 830 |
"from collections import OrderedDict\n", |
| 831 |
"selection_interacts = ToggleButtons(options=OrderedDict([('HandDraw1', hd), ('HandDraw2', hd2), ('PanZoom', pz), \n", |
| 832 |
" ('FastIntervalSelector', int_sel), ('IndexSelector', index_sel),\n", |
| 833 |
" ('BrushIntervalSelector', br_intsel), ('MultiSelector', multi_sel),\n", |
| 834 |
" ('None', None)]))\n", |
| 835 |
"\n", |
| 836 |
"link((selection_interacts, 'value'), (fig, 'interaction'))\n", |
| 837 |
"VBox([deb, fig, selection_interacts], align_self='stretch')" |
| 838 |
] |
| 839 |
}, |
| 840 |
{ |
| 841 |
"cell_type": "code", |
| 842 |
"execution_count": null, |
| 843 |
"metadata": {}, |
| 844 |
"outputs": [], |
| 845 |
"source": [ |
| 846 |
"# Set the scales of lc4 to the ones of lc2 and check if panzoom pans the two.\n", |
| 847 |
"lc4.scales = lc2.scales" |
| 848 |
] |
| 849 |
} |
| 850 |
], |
| 851 |
"metadata": { |
| 852 |
"anaconda-cloud": {}, |
| 853 |
"kernelspec": { |
| 854 |
"display_name": "Python 3", |
| 855 |
"language": "python", |
| 856 |
"name": "python3" |
| 857 |
}, |
| 858 |
"language_info": { |
| 859 |
"codemirror_mode": { |
| 860 |
"name": "ipython", |
| 861 |
"version": 3 |
| 862 |
}, |
| 863 |
"file_extension": ".py", |
| 864 |
"mimetype": "text/x-python", |
| 865 |
"name": "python", |
| 866 |
"nbconvert_exporter": "python", |
| 867 |
"pygments_lexer": "ipython3", |
| 868 |
"version": "3.7.6" |
| 869 |
} |
| 870 |
}, |
| 871 |
"nbformat": 4, |
| 872 |
"nbformat_minor": 2 |
| 873 |
} |