| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": { |
| 6 |
"deletable": true, |
| 7 |
"editable": true |
| 8 |
}, |
| 9 |
"source": [ |
| 10 |
"## ColorScale\n", |
| 11 |
"\n", |
| 12 |
"The colors for the `ColorScale` can be defined one of two ways:\n", |
| 13 |
"- Manually, by setting the scale's `colors` attribute to a list of css colors. They can be either:\n", |
| 14 |
" - html colors (link) `'white'`\n", |
| 15 |
" - hex `'#000000'`\n", |
| 16 |
" - rgb `'rgb(0, 0, 0)'`.\n", |
| 17 |
"\n", |
| 18 |
"```python\n", |
| 19 |
"col_sc = ColorScale(colors=['yellow', 'red'])\n", |
| 20 |
"```\n", |
| 21 |
"- Using one of `bqplot`'s color-schemes. As of now we support all the colorbrewer schemes (link), as well as the matplotlib schemes 'viridis', 'magma', 'inferno' and 'plasma'.\n", |
| 22 |
"```python\n", |
| 23 |
"col_sc = ColorScale(scheme=['viridis'])\n", |
| 24 |
"```\n", |
| 25 |
"\n", |
| 26 |
"The color scale then linearly interpolates between its colors.\n", |
| 27 |
"\n", |
| 28 |
"### ColorAxis\n", |
| 29 |
"\n", |
| 30 |
"A `ColorAxis`, like other `Axis` types, takes a color scale as input. It can then be displayed in a `Figure`.\n", |
| 31 |
"\n", |
| 32 |
"```python\n", |
| 33 |
"ax_col = ColorAxis(scale=col_sc)\n", |
| 34 |
"```" |
| 35 |
] |
| 36 |
}, |
| 37 |
{ |
| 38 |
"cell_type": "code", |
| 39 |
"execution_count": null, |
| 40 |
"metadata": { |
| 41 |
"collapsed": true, |
| 42 |
"deletable": true, |
| 43 |
"editable": true |
| 44 |
}, |
| 45 |
"outputs": [], |
| 46 |
"source": [ |
| 47 |
"import numpy as np\n", |
| 48 |
"\n", |
| 49 |
"import bqplot.pyplot as plt\n", |
| 50 |
"from bqplot import ColorScale, DateColorScale, OrdinalColorScale, ColorAxis" |
| 51 |
] |
| 52 |
}, |
| 53 |
{ |
| 54 |
"cell_type": "code", |
| 55 |
"execution_count": null, |
| 56 |
"metadata": { |
| 57 |
"collapsed": true |
| 58 |
}, |
| 59 |
"outputs": [], |
| 60 |
"source": [ |
| 61 |
"# setup data for plotting\n", |
| 62 |
"np.random.seed(0)\n", |
| 63 |
"n = 100\n", |
| 64 |
"x_data = range(n)\n", |
| 65 |
"y_data = np.cumsum(np.random.randn(n) * 100.0)" |
| 66 |
] |
| 67 |
}, |
| 68 |
{ |
| 69 |
"cell_type": "code", |
| 70 |
"execution_count": null, |
| 71 |
"metadata": { |
| 72 |
"collapsed": false |
| 73 |
}, |
| 74 |
"outputs": [], |
| 75 |
"source": [ |
| 76 |
"def create_fig(color_scale, color_data, fig_margin=None):\n", |
| 77 |
" # allow some margin on right for color bar\n", |
| 78 |
" if fig_margin is None:\n", |
| 79 |
" fig_margin = dict(top=50, bottom=70, left=50, right=100)\n", |
| 80 |
" fig = plt.figure(title='Up and Down', fig_margin=fig_margin)\n", |
| 81 |
" \n", |
| 82 |
" # setup color scale\n", |
| 83 |
" plt.scales(scales={'color': color_scale})\n", |
| 84 |
" \n", |
| 85 |
" # show color bar on right\n", |
| 86 |
" axes_options = {'color': {'orientation': 'vertical', 'side': 'right'}}\n", |
| 87 |
" scat = plt.scatter(x_data, y_data, color=color_data, stroke='black', axes_options=axes_options)\n", |
| 88 |
" return fig" |
| 89 |
] |
| 90 |
}, |
| 91 |
{ |
| 92 |
"cell_type": "code", |
| 93 |
"execution_count": null, |
| 94 |
"metadata": { |
| 95 |
"collapsed": false |
| 96 |
}, |
| 97 |
"outputs": [], |
| 98 |
"source": [ |
| 99 |
"fig = create_fig(ColorScale(), y_data)\n", |
| 100 |
"fig" |
| 101 |
] |
| 102 |
}, |
| 103 |
{ |
| 104 |
"cell_type": "markdown", |
| 105 |
"metadata": { |
| 106 |
"deletable": true, |
| 107 |
"editable": true |
| 108 |
}, |
| 109 |
"source": [ |
| 110 |
"### Attributes\n", |
| 111 |
"`ColorScales` share attributes with the other `Scale` types:\n", |
| 112 |
"- Their domain can be manually constrained with the `min` and `max` attributes\n", |
| 113 |
"- They can be inversed by setting the `reverse` attribute to `True`" |
| 114 |
] |
| 115 |
}, |
| 116 |
{ |
| 117 |
"cell_type": "code", |
| 118 |
"execution_count": null, |
| 119 |
"metadata": { |
| 120 |
"collapsed": true, |
| 121 |
"deletable": true, |
| 122 |
"editable": true |
| 123 |
}, |
| 124 |
"outputs": [], |
| 125 |
"source": [ |
| 126 |
"color_scale = fig.marks[0].scales['color']\n", |
| 127 |
"color_scale.min = 0" |
| 128 |
] |
| 129 |
}, |
| 130 |
{ |
| 131 |
"cell_type": "code", |
| 132 |
"execution_count": null, |
| 133 |
"metadata": { |
| 134 |
"collapsed": true, |
| 135 |
"deletable": true, |
| 136 |
"editable": true |
| 137 |
}, |
| 138 |
"outputs": [], |
| 139 |
"source": [ |
| 140 |
"color_scale.reverse = True" |
| 141 |
] |
| 142 |
}, |
| 143 |
{ |
| 144 |
"cell_type": "markdown", |
| 145 |
"metadata": { |
| 146 |
"deletable": true, |
| 147 |
"editable": true |
| 148 |
}, |
| 149 |
"source": [ |
| 150 |
"#### Mid\n", |
| 151 |
"In addition they also have a `mid` attribute, a value that will be mapped to the middle color. This is especially suited to diverging color schemes." |
| 152 |
] |
| 153 |
}, |
| 154 |
{ |
| 155 |
"cell_type": "code", |
| 156 |
"execution_count": null, |
| 157 |
"metadata": { |
| 158 |
"collapsed": true, |
| 159 |
"deletable": true, |
| 160 |
"editable": true |
| 161 |
}, |
| 162 |
"outputs": [], |
| 163 |
"source": [ |
| 164 |
"color_scale.min = None\n", |
| 165 |
"color_scale.mid = 0" |
| 166 |
] |
| 167 |
}, |
| 168 |
{ |
| 169 |
"cell_type": "markdown", |
| 170 |
"metadata": { |
| 171 |
"deletable": true, |
| 172 |
"editable": true |
| 173 |
}, |
| 174 |
"source": [ |
| 175 |
"## DateColorScale\n", |
| 176 |
"\n", |
| 177 |
"The `DateColorScale` is a color scale for dates. It works in every way like the regular `ColorScale`, except that its `min`, `mid` and `max` attributes — if defined — must be date elements (`datetime`, `numpy` or `pandas`)." |
| 178 |
] |
| 179 |
}, |
| 180 |
{ |
| 181 |
"cell_type": "code", |
| 182 |
"execution_count": null, |
| 183 |
"metadata": { |
| 184 |
"collapsed": false, |
| 185 |
"deletable": true, |
| 186 |
"editable": true |
| 187 |
}, |
| 188 |
"outputs": [], |
| 189 |
"source": [ |
| 190 |
"import pandas as pd\n", |
| 191 |
"\n", |
| 192 |
"fig_margin = dict(top=50, bottom=70, left=50, right=200)\n", |
| 193 |
"date_col_sc = DateColorScale()\n", |
| 194 |
"dates = pd.date_range(start='2015-01-01', periods=n)\n", |
| 195 |
"create_fig(date_col_sc, dates, fig_margin=fig_margin)" |
| 196 |
] |
| 197 |
}, |
| 198 |
{ |
| 199 |
"cell_type": "code", |
| 200 |
"execution_count": null, |
| 201 |
"metadata": { |
| 202 |
"collapsed": true, |
| 203 |
"deletable": true, |
| 204 |
"editable": true |
| 205 |
}, |
| 206 |
"outputs": [], |
| 207 |
"source": [ |
| 208 |
"date_col_sc.min = pd.datetime(2016, 2, 28)" |
| 209 |
] |
| 210 |
}, |
| 211 |
{ |
| 212 |
"cell_type": "markdown", |
| 213 |
"metadata": { |
| 214 |
"deletable": true, |
| 215 |
"editable": true |
| 216 |
}, |
| 217 |
"source": [ |
| 218 |
"## Color Schemes\n", |
| 219 |
"\n", |
| 220 |
"Use the following widgets to browse through the available color schemes" |
| 221 |
] |
| 222 |
}, |
| 223 |
{ |
| 224 |
"cell_type": "code", |
| 225 |
"execution_count": null, |
| 226 |
"metadata": { |
| 227 |
"collapsed": true, |
| 228 |
"deletable": true, |
| 229 |
"editable": true |
| 230 |
}, |
| 231 |
"outputs": [], |
| 232 |
"source": [ |
| 233 |
"from bqplot.market_map import MarketMap\n", |
| 234 |
"\n", |
| 235 |
"from ipywidgets import IntSlider, SelectionSlider, Dropdown\n", |
| 236 |
"from ipywidgets import VBox, HBox, Layout\n", |
| 237 |
"from traitlets import link" |
| 238 |
] |
| 239 |
}, |
| 240 |
{ |
| 241 |
"cell_type": "markdown", |
| 242 |
"metadata": { |
| 243 |
"deletable": true, |
| 244 |
"editable": true |
| 245 |
}, |
| 246 |
"source": [ |
| 247 |
"### Diverging schemes" |
| 248 |
] |
| 249 |
}, |
| 250 |
{ |
| 251 |
"cell_type": "code", |
| 252 |
"execution_count": null, |
| 253 |
"metadata": { |
| 254 |
"collapsed": true, |
| 255 |
"deletable": true, |
| 256 |
"editable": true |
| 257 |
}, |
| 258 |
"outputs": [], |
| 259 |
"source": [ |
| 260 |
"div_schemes = [\n", |
| 261 |
" 'Spectral',\n", |
| 262 |
" 'RdYlGn',\n", |
| 263 |
" 'RdBu',\n", |
| 264 |
" 'PiYG',\n", |
| 265 |
" 'PRGn',\n", |
| 266 |
" 'RdYlBu',\n", |
| 267 |
" 'BrBG',\n", |
| 268 |
" 'RdGy',\n", |
| 269 |
" 'PuOr',\n", |
| 270 |
"]" |
| 271 |
] |
| 272 |
}, |
| 273 |
{ |
| 274 |
"cell_type": "code", |
| 275 |
"execution_count": null, |
| 276 |
"metadata": { |
| 277 |
"collapsed": false, |
| 278 |
"deletable": true, |
| 279 |
"editable": true |
| 280 |
}, |
| 281 |
"outputs": [], |
| 282 |
"source": [ |
| 283 |
"def scheme_inspector(fig, schemes, title=''):\n", |
| 284 |
" '''\n", |
| 285 |
" Takes a Figure and a list of schemes and returns the Figure along with\n", |
| 286 |
" dropdown to go through the different schemes\n", |
| 287 |
" '''\n", |
| 288 |
" \n", |
| 289 |
" # Get the color scale\n", |
| 290 |
" col_sc = fig.marks[0].scales['color']\n", |
| 291 |
" \n", |
| 292 |
" # Create the widgets to select the colorscheme\n", |
| 293 |
" scheme_dd = Dropdown(description='Scheme',\n", |
| 294 |
" options=schemes)\n", |
| 295 |
" \n", |
| 296 |
" def update_scheme(*args):\n", |
| 297 |
" col_sc.scheme = scheme_dd.value\n", |
| 298 |
"\n", |
| 299 |
" scheme_dd.observe(update_scheme, 'value') \n", |
| 300 |
" update_scheme()\n", |
| 301 |
" return VBox([scheme_dd, fig])\n", |
| 302 |
"\n", |
| 303 |
"scheme_inspector(create_fig(ColorScale(), y_data), div_schemes)" |
| 304 |
] |
| 305 |
}, |
| 306 |
{ |
| 307 |
"cell_type": "markdown", |
| 308 |
"metadata": { |
| 309 |
"deletable": true, |
| 310 |
"editable": true |
| 311 |
}, |
| 312 |
"source": [ |
| 313 |
"### Non-diverging schemes" |
| 314 |
] |
| 315 |
}, |
| 316 |
{ |
| 317 |
"cell_type": "code", |
| 318 |
"execution_count": null, |
| 319 |
"metadata": { |
| 320 |
"collapsed": true, |
| 321 |
"deletable": true, |
| 322 |
"editable": true |
| 323 |
}, |
| 324 |
"outputs": [], |
| 325 |
"source": [ |
| 326 |
"lin_schemes = [\n", |
| 327 |
" 'OrRd',\n", |
| 328 |
" 'PuBu',\n", |
| 329 |
" 'BuPu',\n", |
| 330 |
" 'Oranges',\n", |
| 331 |
" 'BuGn',\n", |
| 332 |
" 'YlOrBr',\n", |
| 333 |
" 'YlGn',\n", |
| 334 |
" 'Reds',\n", |
| 335 |
" 'RdPu',\n", |
| 336 |
" 'Greens',\n", |
| 337 |
" 'YlGnBu',\n", |
| 338 |
" 'Purples',\n", |
| 339 |
" 'GnBu',\n", |
| 340 |
" 'Greys',\n", |
| 341 |
" 'YlOrRd',\n", |
| 342 |
" 'PuRd',\n", |
| 343 |
" 'Blues',\n", |
| 344 |
" 'PuBuGn',\n", |
| 345 |
" 'viridis',\n", |
| 346 |
" 'plasma',\n", |
| 347 |
" 'inferno',\n", |
| 348 |
" 'magma'\n", |
| 349 |
"]" |
| 350 |
] |
| 351 |
}, |
| 352 |
{ |
| 353 |
"cell_type": "code", |
| 354 |
"execution_count": null, |
| 355 |
"metadata": { |
| 356 |
"collapsed": false, |
| 357 |
"deletable": true, |
| 358 |
"editable": true |
| 359 |
}, |
| 360 |
"outputs": [], |
| 361 |
"source": [ |
| 362 |
"scheme_inspector(create_fig(ColorScale(), y_data), lin_schemes, title='Non-diverging schemes')" |
| 363 |
] |
| 364 |
}, |
| 365 |
{ |
| 366 |
"cell_type": "markdown", |
| 367 |
"metadata": { |
| 368 |
"deletable": true, |
| 369 |
"editable": true |
| 370 |
}, |
| 371 |
"source": [ |
| 372 |
"## OrdinalColorScale\n", |
| 373 |
"\n", |
| 374 |
"The `OrdinalColorScale` is a color scale for categorical data, i.e. data that does not have an intrinsic order.\n", |
| 375 |
"\n", |
| 376 |
"The scale colors may be specified by the user, or chosen from a set scheme. As of now, the supported color schemes are the colorbrewer categorical schemes, listed here along with their maximum number of colors." |
| 377 |
] |
| 378 |
}, |
| 379 |
{ |
| 380 |
"cell_type": "code", |
| 381 |
"execution_count": null, |
| 382 |
"metadata": { |
| 383 |
"collapsed": true, |
| 384 |
"deletable": true, |
| 385 |
"editable": true |
| 386 |
}, |
| 387 |
"outputs": [], |
| 388 |
"source": [ |
| 389 |
"ord_schemes = {\n", |
| 390 |
" 'Set2': 8,\n", |
| 391 |
" 'Accent': 8,\n", |
| 392 |
" 'Set1': 9,\n", |
| 393 |
" 'Set3': 12,\n", |
| 394 |
" 'Dark2': 8,\n", |
| 395 |
" 'Paired': 12,\n", |
| 396 |
" 'Pastel2': 8,\n", |
| 397 |
" 'Pastel1': 9,\n", |
| 398 |
"}" |
| 399 |
] |
| 400 |
}, |
| 401 |
{ |
| 402 |
"cell_type": "code", |
| 403 |
"execution_count": null, |
| 404 |
"metadata": { |
| 405 |
"collapsed": true, |
| 406 |
"deletable": true, |
| 407 |
"editable": true |
| 408 |
}, |
| 409 |
"outputs": [], |
| 410 |
"source": [ |
| 411 |
"def partition(array, n_groups):\n", |
| 412 |
" n_elements = len(array)\n", |
| 413 |
" if n_groups > n_elements:\n", |
| 414 |
" return np.arange(n_elements)\n", |
| 415 |
" n_per_group = n_elements // n_groups + (n_elements % n_groups > 0)\n", |
| 416 |
" return np.tile(range(1, n_groups+1), n_per_group)[:n_elements]" |
| 417 |
] |
| 418 |
}, |
| 419 |
{ |
| 420 |
"cell_type": "code", |
| 421 |
"execution_count": null, |
| 422 |
"metadata": { |
| 423 |
"collapsed": false, |
| 424 |
"deletable": true, |
| 425 |
"editable": true, |
| 426 |
"scrolled": false |
| 427 |
}, |
| 428 |
"outputs": [], |
| 429 |
"source": [ |
| 430 |
"# Define the control widgets\n", |
| 431 |
"n_groups_slider = IntSlider(description='n colors', min=3)\n", |
| 432 |
"scheme_dd = Dropdown(description='Scheme',\n", |
| 433 |
" options=ord_schemes)\n", |
| 434 |
" \n", |
| 435 |
"def update_scheme(*args):\n", |
| 436 |
" col_sc.scheme = scheme_dd.label\n", |
| 437 |
" ax_c.label = scheme_dd.label\n", |
| 438 |
" n_groups_slider.max = scheme_dd.value\n", |
| 439 |
"\n", |
| 440 |
"def update_categories(*args):\n", |
| 441 |
" groups = partition(names, n_groups_slider.value)\n", |
| 442 |
" market_map.color = groups\n", |
| 443 |
" market_map.groups = groups\n", |
| 444 |
"\n", |
| 445 |
"n_groups_slider.observe(update_categories, 'value')\n", |
| 446 |
"scheme_dd.observe(update_scheme)\n", |
| 447 |
"\n", |
| 448 |
"# Define the bqplot marketmap\n", |
| 449 |
"names = range(100)\n", |
| 450 |
"\n", |
| 451 |
"col_sc = OrdinalColorScale()\n", |
| 452 |
"ax_c = ColorAxis(scale=col_sc)\n", |
| 453 |
"\n", |
| 454 |
"market_map = MarketMap(names=names,\n", |
| 455 |
" display_text=['' for _ in names],\n", |
| 456 |
" scales={'color': col_sc}, axes=[ax_c],\n", |
| 457 |
" layout=Layout(min_width='800px', min_height='600px'))\n", |
| 458 |
"update_scheme()\n", |
| 459 |
"update_categories()\n", |
| 460 |
"\n", |
| 461 |
"VBox([HBox([scheme_dd, n_groups_slider]), market_map])" |
| 462 |
] |
| 463 |
} |
| 464 |
], |
| 465 |
"metadata": { |
| 466 |
"kernelspec": { |
| 467 |
"display_name": "Python 3", |
| 468 |
"language": "python", |
| 469 |
"name": "python3" |
| 470 |
}, |
| 471 |
"language_info": { |
| 472 |
"codemirror_mode": { |
| 473 |
"name": "ipython", |
| 474 |
"version": 3 |
| 475 |
}, |
| 476 |
"file_extension": ".py", |
| 477 |
"mimetype": "text/x-python", |
| 478 |
"name": "python", |
| 479 |
"nbconvert_exporter": "python", |
| 480 |
"pygments_lexer": "ipython3", |
| 481 |
"version": "3.6.2" |
| 482 |
} |
| 483 |
}, |
| 484 |
"nbformat": 4, |
| 485 |
"nbformat_minor": 2 |
| 486 |
} |