| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"metadata": {}, |
| 7 |
"outputs": [], |
| 8 |
"source": [ |
| 9 |
"from __future__ import print_function\n", |
| 10 |
"from bqplot import *\n", |
| 11 |
"import numpy as np\n", |
| 12 |
"import pandas as pd\n", |
| 13 |
"from ipywidgets import Layout, Dropdown, Button\n", |
| 14 |
"from ipywidgets import Image as ImageIpy" |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "markdown", |
| 19 |
"metadata": {}, |
| 20 |
"source": [ |
| 21 |
"## Scatter Chart" |
| 22 |
] |
| 23 |
}, |
| 24 |
{ |
| 25 |
"cell_type": "markdown", |
| 26 |
"metadata": {}, |
| 27 |
"source": [ |
| 28 |
"#### Scatter Chart Selections\n", |
| 29 |
"\n", |
| 30 |
"Click a point on the `Scatter` plot to select it. Now, run the cell below to check the selection. After you've done this, try holding the `ctrl` (or `command` key on Mac) and clicking another point. Clicking the background will reset the selection." |
| 31 |
] |
| 32 |
}, |
| 33 |
{ |
| 34 |
"cell_type": "code", |
| 35 |
"execution_count": null, |
| 36 |
"metadata": {}, |
| 37 |
"outputs": [], |
| 38 |
"source": [ |
| 39 |
"x_sc = LinearScale()\n", |
| 40 |
"y_sc = LinearScale()\n", |
| 41 |
"\n", |
| 42 |
"x_data = np.arange(20)\n", |
| 43 |
"y_data = np.random.randn(20)\n", |
| 44 |
"\n", |
| 45 |
"scatter_chart = Scatter(x=x_data, y=y_data, scales= {'x': x_sc, 'y': y_sc}, colors=['dodgerblue'],\n", |
| 46 |
" interactions={'click': 'select'},\n", |
| 47 |
" selected_style={'opacity': 1.0, 'fill': 'DarkOrange', 'stroke': 'Red'},\n", |
| 48 |
" unselected_style={'opacity': 0.5})\n", |
| 49 |
"\n", |
| 50 |
"ax_x = Axis(scale=x_sc)\n", |
| 51 |
"ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n", |
| 52 |
"\n", |
| 53 |
"Figure(marks=[scatter_chart], axes=[ax_x, ax_y])" |
| 54 |
] |
| 55 |
}, |
| 56 |
{ |
| 57 |
"cell_type": "code", |
| 58 |
"execution_count": null, |
| 59 |
"metadata": {}, |
| 60 |
"outputs": [], |
| 61 |
"source": [ |
| 62 |
"scatter_chart.selected" |
| 63 |
] |
| 64 |
}, |
| 65 |
{ |
| 66 |
"cell_type": "markdown", |
| 67 |
"metadata": {}, |
| 68 |
"source": [ |
| 69 |
"Alternately, the `selected` attribute can be directly set on the Python side (try running the cell below):" |
| 70 |
] |
| 71 |
}, |
| 72 |
{ |
| 73 |
"cell_type": "code", |
| 74 |
"execution_count": null, |
| 75 |
"metadata": {}, |
| 76 |
"outputs": [], |
| 77 |
"source": [ |
| 78 |
"scatter_chart.selected = [1, 2, 3]" |
| 79 |
] |
| 80 |
}, |
| 81 |
{ |
| 82 |
"cell_type": "markdown", |
| 83 |
"metadata": {}, |
| 84 |
"source": [ |
| 85 |
"#### Scatter Chart Interactions and Tooltips" |
| 86 |
] |
| 87 |
}, |
| 88 |
{ |
| 89 |
"cell_type": "code", |
| 90 |
"execution_count": null, |
| 91 |
"metadata": { |
| 92 |
"scrolled": false |
| 93 |
}, |
| 94 |
"outputs": [], |
| 95 |
"source": [ |
| 96 |
"x_sc = LinearScale()\n", |
| 97 |
"y_sc = LinearScale()\n", |
| 98 |
"\n", |
| 99 |
"x_data = np.arange(20)\n", |
| 100 |
"y_data = np.random.randn(20)\n", |
| 101 |
"\n", |
| 102 |
"dd = Dropdown(options=['First', 'Second', 'Third', 'Fourth'])\n", |
| 103 |
"scatter_chart = Scatter(x=x_data, y=y_data, scales= {'x': x_sc, 'y': y_sc}, colors=['dodgerblue'],\n", |
| 104 |
" names=np.arange(100, 200), names_unique=False, display_names=False, display_legend=True,\n", |
| 105 |
" labels=['Blue'])\n", |
| 106 |
"ins = Button(icon='fa-legal')\n", |
| 107 |
"scatter_chart.tooltip = ins\n", |
| 108 |
"line = Lines(x=x_data, y=y_data, scales= {'x': x_sc, 'y': y_sc}, colors=['dodgerblue'])\n", |
| 109 |
"scatter_chart2 = Scatter(x=x_data, y=np.random.randn(20), \n", |
| 110 |
" scales= {'x': x_sc, 'y': y_sc}, colors=['orangered'],\n", |
| 111 |
" tooltip=dd, names=np.arange(100, 200), names_unique=False, display_names=False, \n", |
| 112 |
" display_legend=True, labels=['Red'])\n", |
| 113 |
"\n", |
| 114 |
"ax_x = Axis(scale=x_sc)\n", |
| 115 |
"ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n", |
| 116 |
"\n", |
| 117 |
"fig = Figure(marks=[scatter_chart, scatter_chart2, line], axes=[ax_x, ax_y])\n", |
| 118 |
"fig" |
| 119 |
] |
| 120 |
}, |
| 121 |
{ |
| 122 |
"cell_type": "code", |
| 123 |
"execution_count": null, |
| 124 |
"metadata": {}, |
| 125 |
"outputs": [], |
| 126 |
"source": [ |
| 127 |
"def print_event(self, target):\n", |
| 128 |
" print(target)\n", |
| 129 |
"\n", |
| 130 |
"# Adding call back to scatter events\n", |
| 131 |
"# print custom mssg on hover and background click of Blue Scatter\n", |
| 132 |
"scatter_chart.on_hover(print_event)\n", |
| 133 |
"scatter_chart.on_background_click(print_event)\n", |
| 134 |
"\n", |
| 135 |
"# print custom mssg on click of an element or legend of Red Scatter\n", |
| 136 |
"scatter_chart2.on_element_click(print_event)\n", |
| 137 |
"scatter_chart2.on_legend_click(print_event)\n", |
| 138 |
"line.on_element_click(print_event)" |
| 139 |
] |
| 140 |
}, |
| 141 |
{ |
| 142 |
"cell_type": "code", |
| 143 |
"execution_count": null, |
| 144 |
"metadata": {}, |
| 145 |
"outputs": [], |
| 146 |
"source": [ |
| 147 |
"# Changing interaction from hover to click for tooltip\n", |
| 148 |
"scatter_chart.interactions = {'click': 'tooltip'}" |
| 149 |
] |
| 150 |
}, |
| 151 |
{ |
| 152 |
"cell_type": "code", |
| 153 |
"execution_count": null, |
| 154 |
"metadata": {}, |
| 155 |
"outputs": [], |
| 156 |
"source": [ |
| 157 |
"# Adding figure as tooltip\n", |
| 158 |
"x_sc = LinearScale()\n", |
| 159 |
"y_sc = LinearScale()\n", |
| 160 |
"\n", |
| 161 |
"x_data = np.arange(10)\n", |
| 162 |
"y_data = np.random.randn(10)\n", |
| 163 |
"\n", |
| 164 |
"lc = Lines(x=x_data, y=y_data, scales={'x': x_sc, 'y':y_sc})\n", |
| 165 |
"ax_x = Axis(scale=x_sc)\n", |
| 166 |
"ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n", |
| 167 |
"tooltip_fig = Figure(marks=[lc], axes=[ax_x, ax_y], layout=Layout(min_width='600px'))\n", |
| 168 |
"\n", |
| 169 |
"scatter_chart.tooltip = tooltip_fig" |
| 170 |
] |
| 171 |
}, |
| 172 |
{ |
| 173 |
"cell_type": "markdown", |
| 174 |
"metadata": {}, |
| 175 |
"source": [ |
| 176 |
"## Image\n", |
| 177 |
"\n", |
| 178 |
"For images, `on_element_click` returns the location of the mouse click. " |
| 179 |
] |
| 180 |
}, |
| 181 |
{ |
| 182 |
"cell_type": "code", |
| 183 |
"execution_count": null, |
| 184 |
"metadata": {}, |
| 185 |
"outputs": [], |
| 186 |
"source": [ |
| 187 |
"i = ImageIpy.from_file(os.path.abspath('../data_files/trees.jpg'))\n", |
| 188 |
"bqi = Image(image=i, scales={'x': x_sc, 'y': y_sc}, x=(0, 10), y=(-1, 1))\n", |
| 189 |
"\n", |
| 190 |
"fig_image = Figure(marks=[bqi], axes=[ax_x, ax_y])\n", |
| 191 |
"fig_image" |
| 192 |
] |
| 193 |
}, |
| 194 |
{ |
| 195 |
"cell_type": "code", |
| 196 |
"execution_count": null, |
| 197 |
"metadata": {}, |
| 198 |
"outputs": [], |
| 199 |
"source": [ |
| 200 |
"bqi.on_element_click(print_event)" |
| 201 |
] |
| 202 |
}, |
| 203 |
{ |
| 204 |
"cell_type": "markdown", |
| 205 |
"metadata": {}, |
| 206 |
"source": [ |
| 207 |
"## Line Chart" |
| 208 |
] |
| 209 |
}, |
| 210 |
{ |
| 211 |
"cell_type": "code", |
| 212 |
"execution_count": null, |
| 213 |
"metadata": {}, |
| 214 |
"outputs": [], |
| 215 |
"source": [ |
| 216 |
"# Adding default tooltip to Line Chart\n", |
| 217 |
"x_sc = LinearScale()\n", |
| 218 |
"y_sc = LinearScale()\n", |
| 219 |
"\n", |
| 220 |
"x_data = np.arange(100)\n", |
| 221 |
"y_data = np.random.randn(3, 100)\n", |
| 222 |
"\n", |
| 223 |
"def_tt = Tooltip(fields=['name', 'index'], formats=['', '.2f'], labels=['id', 'line_num'])\n", |
| 224 |
"line_chart = Lines(x=x_data, y=y_data, scales= {'x': x_sc, 'y': y_sc}, \n", |
| 225 |
" tooltip=def_tt, display_legend=True, labels=[\"line 1\", \"line 2\", \"line 3\"] )\n", |
| 226 |
"\n", |
| 227 |
"ax_x = Axis(scale=x_sc)\n", |
| 228 |
"ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n", |
| 229 |
"\n", |
| 230 |
"Figure(marks=[line_chart], axes=[ax_x, ax_y])" |
| 231 |
] |
| 232 |
}, |
| 233 |
{ |
| 234 |
"cell_type": "code", |
| 235 |
"execution_count": null, |
| 236 |
"metadata": {}, |
| 237 |
"outputs": [], |
| 238 |
"source": [ |
| 239 |
"# Adding call back to print event when legend or the line is clicked\n", |
| 240 |
"line_chart.on_legend_click(print_event)\n", |
| 241 |
"line_chart.on_element_click(print_event)" |
| 242 |
] |
| 243 |
}, |
| 244 |
{ |
| 245 |
"cell_type": "markdown", |
| 246 |
"metadata": {}, |
| 247 |
"source": [ |
| 248 |
"## Bar Chart" |
| 249 |
] |
| 250 |
}, |
| 251 |
{ |
| 252 |
"cell_type": "code", |
| 253 |
"execution_count": null, |
| 254 |
"metadata": {}, |
| 255 |
"outputs": [], |
| 256 |
"source": [ |
| 257 |
"# Adding interaction to select bar on click for Bar Chart\n", |
| 258 |
"x_sc = OrdinalScale()\n", |
| 259 |
"y_sc = LinearScale()\n", |
| 260 |
"\n", |
| 261 |
"x_data = np.arange(10)\n", |
| 262 |
"y_data = np.random.randn(2, 10)\n", |
| 263 |
"\n", |
| 264 |
"bar_chart = Bars(x=x_data, y=[y_data[0, :].tolist(), y_data[1, :].tolist()], scales= {'x': x_sc, 'y': y_sc},\n", |
| 265 |
" interactions={'click': 'select'},\n", |
| 266 |
" selected_style={'stroke': 'orange', 'fill': 'red'},\n", |
| 267 |
" labels=['Level 1', 'Level 2'],\n", |
| 268 |
" display_legend=True)\n", |
| 269 |
"ax_x = Axis(scale=x_sc)\n", |
| 270 |
"ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n", |
| 271 |
"\n", |
| 272 |
"Figure(marks=[bar_chart], axes=[ax_x, ax_y])" |
| 273 |
] |
| 274 |
}, |
| 275 |
{ |
| 276 |
"cell_type": "code", |
| 277 |
"execution_count": null, |
| 278 |
"metadata": {}, |
| 279 |
"outputs": [], |
| 280 |
"source": [ |
| 281 |
"# Adding a tooltip on hover in addition to select on click\n", |
| 282 |
"def_tt = Tooltip(fields=['x', 'y'], formats=['', '.2f'])\n", |
| 283 |
"bar_chart.tooltip=def_tt\n", |
| 284 |
"bar_chart.interactions = {\n", |
| 285 |
" 'legend_hover': 'highlight_axes',\n", |
| 286 |
" 'hover': 'tooltip', \n", |
| 287 |
" 'click': 'select',\n", |
| 288 |
"}" |
| 289 |
] |
| 290 |
}, |
| 291 |
{ |
| 292 |
"cell_type": "code", |
| 293 |
"execution_count": null, |
| 294 |
"metadata": {}, |
| 295 |
"outputs": [], |
| 296 |
"source": [ |
| 297 |
"# Changing tooltip to be on click\n", |
| 298 |
"bar_chart.interactions = {'click': 'tooltip'}" |
| 299 |
] |
| 300 |
}, |
| 301 |
{ |
| 302 |
"cell_type": "code", |
| 303 |
"execution_count": null, |
| 304 |
"metadata": {}, |
| 305 |
"outputs": [], |
| 306 |
"source": [ |
| 307 |
"# Call back on legend being clicked\n", |
| 308 |
"bar_chart.type='grouped'\n", |
| 309 |
"bar_chart.on_legend_click(print_event)" |
| 310 |
] |
| 311 |
}, |
| 312 |
{ |
| 313 |
"cell_type": "markdown", |
| 314 |
"metadata": {}, |
| 315 |
"source": [ |
| 316 |
"## Histogram" |
| 317 |
] |
| 318 |
}, |
| 319 |
{ |
| 320 |
"cell_type": "code", |
| 321 |
"execution_count": null, |
| 322 |
"metadata": {}, |
| 323 |
"outputs": [], |
| 324 |
"source": [ |
| 325 |
"# Adding tooltip for Histogram\n", |
| 326 |
"x_sc = LinearScale()\n", |
| 327 |
"y_sc = LinearScale()\n", |
| 328 |
"\n", |
| 329 |
"sample_data = np.random.randn(100)\n", |
| 330 |
"\n", |
| 331 |
"def_tt = Tooltip(formats=['', '.2f'], fields=['count', 'midpoint'])\n", |
| 332 |
"hist = Hist(sample=sample_data, scales= {'sample': x_sc, 'count': y_sc},\n", |
| 333 |
" tooltip=def_tt, display_legend=True, labels=['Test Hist'], select_bars=True)\n", |
| 334 |
"ax_x = Axis(scale=x_sc, tick_format='0.2f')\n", |
| 335 |
"ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n", |
| 336 |
"\n", |
| 337 |
"Figure(marks=[hist], axes=[ax_x, ax_y])" |
| 338 |
] |
| 339 |
}, |
| 340 |
{ |
| 341 |
"cell_type": "code", |
| 342 |
"execution_count": null, |
| 343 |
"metadata": {}, |
| 344 |
"outputs": [], |
| 345 |
"source": [ |
| 346 |
"# Changing tooltip to be displayed on click\n", |
| 347 |
"hist.interactions = {'click': 'tooltip'}" |
| 348 |
] |
| 349 |
}, |
| 350 |
{ |
| 351 |
"cell_type": "code", |
| 352 |
"execution_count": null, |
| 353 |
"metadata": {}, |
| 354 |
"outputs": [], |
| 355 |
"source": [ |
| 356 |
"# Changing tooltip to be on click of legend\n", |
| 357 |
"hist.interactions = {'legend_click': 'tooltip'}" |
| 358 |
] |
| 359 |
}, |
| 360 |
{ |
| 361 |
"cell_type": "markdown", |
| 362 |
"metadata": { |
| 363 |
"collapsed": true |
| 364 |
}, |
| 365 |
"source": [ |
| 366 |
"## Pie Chart" |
| 367 |
] |
| 368 |
}, |
| 369 |
{ |
| 370 |
"cell_type": "markdown", |
| 371 |
"metadata": {}, |
| 372 |
"source": [ |
| 373 |
"Set up a pie chart with click to show the tooltip." |
| 374 |
] |
| 375 |
}, |
| 376 |
{ |
| 377 |
"cell_type": "code", |
| 378 |
"execution_count": null, |
| 379 |
"metadata": {}, |
| 380 |
"outputs": [], |
| 381 |
"source": [ |
| 382 |
"pie_data = np.abs(np.random.randn(10))\n", |
| 383 |
"\n", |
| 384 |
"sc = ColorScale(scheme='Reds')\n", |
| 385 |
"tooltip_widget = Tooltip(fields=['size', 'index', 'color'], formats=['0.2f', '', '0.2f'])\n", |
| 386 |
"pie = Pie(sizes=pie_data, scales={'color': sc}, color=np.random.randn(10), \n", |
| 387 |
" tooltip=tooltip_widget, interactions = {'click': 'tooltip'}, selected_style={'fill': 'red'})\n", |
| 388 |
"\n", |
| 389 |
"pie.selected_style = {\"opacity\": \"1\", \"stroke\": \"white\", \"stroke-width\": \"2\"}\n", |
| 390 |
"pie.unselected_style = {\"opacity\": \"0.2\"}\n", |
| 391 |
"\n", |
| 392 |
"Figure(marks=[pie])" |
| 393 |
] |
| 394 |
}, |
| 395 |
{ |
| 396 |
"cell_type": "code", |
| 397 |
"execution_count": null, |
| 398 |
"metadata": {}, |
| 399 |
"outputs": [], |
| 400 |
"source": [ |
| 401 |
"# Changing interaction to select on click and tooltip on hover\n", |
| 402 |
"pie.interactions = {'click': 'select', 'hover': 'tooltip'}" |
| 403 |
] |
| 404 |
} |
| 405 |
], |
| 406 |
"metadata": { |
| 407 |
"anaconda-cloud": {}, |
| 408 |
"kernelspec": { |
| 409 |
"display_name": "Python 3", |
| 410 |
"language": "python", |
| 411 |
"name": "python3" |
| 412 |
}, |
| 413 |
"language_info": { |
| 414 |
"codemirror_mode": { |
| 415 |
"name": "ipython", |
| 416 |
"version": 3 |
| 417 |
}, |
| 418 |
"file_extension": ".py", |
| 419 |
"mimetype": "text/x-python", |
| 420 |
"name": "python", |
| 421 |
"nbconvert_exporter": "python", |
| 422 |
"pygments_lexer": "ipython3", |
| 423 |
"version": "3.7.6" |
| 424 |
} |
| 425 |
}, |
| 426 |
"nbformat": 4, |
| 427 |
"nbformat_minor": 2 |
| 428 |
} |