{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from bqplot import *\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ColorScale\n", "\n", "The colors for the `ColorScale` can be defined one of two ways:\n", "- Manually, by setting the scale's `colors` attribute to a list of css colors. They can be either:\n", " - html colors (link) `'white'`\n", " - hex `'#000000'`\n", " - rgb `'rgb(0, 0, 0)'`.\n", "\n", "```python\n", "col_sc = ColorScale(colors=['yellow', 'red'])\n", "```\n", "- 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", "```python\n", "col_sc = ColorScale(scheme=['viridis'])\n", "```\n", "\n", "The color scale then linearly interpolates between its colors.\n", "\n", "### ColorAxis\n", "\n", "A `ColorAxis`, like other `Axis` types, takes a color scale as input. It can then be displayed in a `Figure`.\n", "\n", "```python\n", "ax_col = ColorAxis(scale=col_sc)\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2e5038105c014af095c616ef952c0874", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type Figure.

\n", "

\n", " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another notebook frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "Figure(axes=[Axis(label_location='end', num_ticks=10, scale=LinearScale()), Axis(orientation='vertical', scale=LinearScale(), side='left'), ColorAxis(orientation='vertical', scale=ColorScale(), side='right')], fig_margin={'top': 50, 'right': 100, 'left': 50, 'bottom': 70}, layout=Layout(min_width=u'125px'), marks=[Scatter(color=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]), colors=['steelblue'], interactions={'hover': 'tooltip'}, scales={'y': LinearScale(), 'x': LinearScale(), 'color': ColorScale()}, scales_metadata={'opacity': {'dimension': 'opacity'}, 'y': {'orientation': 'vertical', 'dimension': 'y'}, 'color': {'dimension': 'color'}, 'x': {'orientation': 'horizontal', 'dimension': 'x'}, 'skew': {'dimension': 'skew'}, 'rotation': {'dimension': 'rotation'}, 'size': {'dimension': 'size'}}, stroke='black', tooltip_style={'opacity': 0.9}, x=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n", " 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n", " 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), y=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]))], scale_x=LinearScale(allow_padding=False, max=1.0, min=0.0), scale_y=LinearScale(allow_padding=False, max=1.0, min=0.0), title=u'Up and Down')" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "size = 100\n", "np.random.seed(0)\n", "x_data = range(size)\n", "y_data = np.cumsum(np.random.randn(size) * 100.0)\n", "\n", "def create_figure(color_scale, color_data):\n", "\n", " scales = {'x': LinearScale(), 'y': LinearScale(), 'color': color_scale}\n", " scatter = Scatter(x=x_data, y=y_data, color=color_data,\n", " scales=scales, \n", " stroke='black')\n", " ax_y = Axis(label='Price', scale=scales['y'], \n", " orientation='vertical', side='left')\n", " ax_x = Axis(label='Day', scale=scales['x'], num_ticks=10, label_location='end')\n", " ax_c = ColorAxis(scale=color_scale, label='Returns', orientation='vertical', side='right')\n", " ax_y = Axis(scale=scales['y'], \n", " orientation='vertical', side='left')\n", " ax_x = Axis(scale=scales['x'], num_ticks=10, label_location='end')\n", " ax_c = ColorAxis(scale=color_scale, orientation='vertical', side='right')\n", "\n", " m_chart = dict(top=50, bottom=70, left=50, right=100)\n", "\n", " return Figure(axes=[ax_x, ax_y, ax_c], title='Up and Down',\n", " marks=[scatter], fig_margin=m_chart,)\n", "\n", "col_sc = ColorScale()\n", "create_figure(col_sc, y_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Attributes\n", "`ColorScales` share attributes with the other `Scale` types:\n", "- Their domain can be manually constrained with the `min` and `max` attributes\n", "- They can be inversed by setting the `reverse` attribute to `True`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "col_sc.min = 0" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "col_sc.reverse = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Mid\n", "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." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "col_sc.min = None\n", "col_sc.mid = 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DateColorScale\n", "\n", "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`)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "1b27aaa240d64c1682962eafcac8f2d5", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type Figure.

\n", "

\n", " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another notebook frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "Figure(axes=[Axis(label_location='end', num_ticks=10, scale=LinearScale()), Axis(orientation='vertical', scale=LinearScale(), side='left'), ColorAxis(orientation='vertical', scale=DateColorScale(), side='right')], fig_margin={'top': 50, 'right': 100, 'left': 50, 'bottom': 70}, layout=Layout(min_width=u'125px'), marks=[Scatter(color=array(['2007-01-01T00:00:00.000000000', '2007-01-02T00:00:00.000000000',\n", " '2007-01-03T00:00:00.000000000', '2007-01-04T00:00:00.000000000',\n", " '2007-01-05T00:00:00.000000000', '2007-01-06T00:00:00.000000000',\n", " '2007-01-07T00:00:00.000000000', '2007-01-08T00:00:00.000000000',\n", " '2007-01-09T00:00:00.000000000', '2007-01-10T00:00:00.000000000',\n", " '2007-01-11T00:00:00.000000000', '2007-01-12T00:00:00.000000000',\n", " '2007-01-13T00:00:00.000000000', '2007-01-14T00:00:00.000000000',\n", " '2007-01-15T00:00:00.000000000', '2007-01-16T00:00:00.000000000',\n", " '2007-01-17T00:00:00.000000000', '2007-01-18T00:00:00.000000000',\n", " '2007-01-19T00:00:00.000000000', '2007-01-20T00:00:00.000000000',\n", " '2007-01-21T00:00:00.000000000', '2007-01-22T00:00:00.000000000',\n", " '2007-01-23T00:00:00.000000000', '2007-01-24T00:00:00.000000000',\n", " '2007-01-25T00:00:00.000000000', '2007-01-26T00:00:00.000000000',\n", " '2007-01-27T00:00:00.000000000', '2007-01-28T00:00:00.000000000',\n", " '2007-01-29T00:00:00.000000000', '2007-01-30T00:00:00.000000000',\n", " '2007-01-31T00:00:00.000000000', '2007-02-01T00:00:00.000000000',\n", " '2007-02-02T00:00:00.000000000', '2007-02-03T00:00:00.000000000',\n", " '2007-02-04T00:00:00.000000000', '2007-02-05T00:00:00.000000000',\n", " '2007-02-06T00:00:00.000000000', '2007-02-07T00:00:00.000000000',\n", " '2007-02-08T00:00:00.000000000', '2007-02-09T00:00:00.000000000',\n", " '2007-02-10T00:00:00.000000000', '2007-02-11T00:00:00.000000000',\n", " '2007-02-12T00:00:00.000000000', '2007-02-13T00:00:00.000000000',\n", " '2007-02-14T00:00:00.000000000', '2007-02-15T00:00:00.000000000',\n", " '2007-02-16T00:00:00.000000000', '2007-02-17T00:00:00.000000000',\n", " '2007-02-18T00:00:00.000000000', '2007-02-19T00:00:00.000000000',\n", " '2007-02-20T00:00:00.000000000', '2007-02-21T00:00:00.000000000',\n", " '2007-02-22T00:00:00.000000000', '2007-02-23T00:00:00.000000000',\n", " '2007-02-24T00:00:00.000000000', '2007-02-25T00:00:00.000000000',\n", " '2007-02-26T00:00:00.000000000', '2007-02-27T00:00:00.000000000',\n", " '2007-02-28T00:00:00.000000000', '2007-03-01T00:00:00.000000000',\n", " '2007-03-02T00:00:00.000000000', '2007-03-03T00:00:00.000000000',\n", " '2007-03-04T00:00:00.000000000', '2007-03-05T00:00:00.000000000',\n", " '2007-03-06T00:00:00.000000000', '2007-03-07T00:00:00.000000000',\n", " '2007-03-08T00:00:00.000000000', '2007-03-09T00:00:00.000000000',\n", " '2007-03-10T00:00:00.000000000', '2007-03-11T00:00:00.000000000',\n", " '2007-03-12T00:00:00.000000000', '2007-03-13T00:00:00.000000000',\n", " '2007-03-14T00:00:00.000000000', '2007-03-15T00:00:00.000000000',\n", " '2007-03-16T00:00:00.000000000', '2007-03-17T00:00:00.000000000',\n", " '2007-03-18T00:00:00.000000000', '2007-03-19T00:00:00.000000000',\n", " '2007-03-20T00:00:00.000000000', '2007-03-21T00:00:00.000000000',\n", " '2007-03-22T00:00:00.000000000', '2007-03-23T00:00:00.000000000',\n", " '2007-03-24T00:00:00.000000000', '2007-03-25T00:00:00.000000000',\n", " '2007-03-26T00:00:00.000000000', '2007-03-27T00:00:00.000000000',\n", " '2007-03-28T00:00:00.000000000', '2007-03-29T00:00:00.000000000',\n", " '2007-03-30T00:00:00.000000000', '2007-03-31T00:00:00.000000000',\n", " '2007-04-01T00:00:00.000000000', '2007-04-02T00:00:00.000000000',\n", " '2007-04-03T00:00:00.000000000', '2007-04-04T00:00:00.000000000',\n", " '2007-04-05T00:00:00.000000000', '2007-04-06T00:00:00.000000000',\n", " '2007-04-07T00:00:00.000000000', '2007-04-08T00:00:00.000000000',\n", " '2007-04-09T00:00:00.000000000', '2007-04-10T00:00:00.000000000'], dtype='datetime64[ns]'), colors=['steelblue'], interactions={'hover': 'tooltip'}, scales={'y': LinearScale(), 'x': LinearScale(), 'color': DateColorScale()}, scales_metadata={'opacity': {'dimension': 'opacity'}, 'y': {'orientation': 'vertical', 'dimension': 'y'}, 'color': {'dimension': 'color'}, 'x': {'orientation': 'horizontal', 'dimension': 'x'}, 'skew': {'dimension': 'skew'}, 'rotation': {'dimension': 'rotation'}, 'size': {'dimension': 'size'}}, stroke='black', tooltip_style={'opacity': 0.9}, x=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n", " 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n", " 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), y=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]))], scale_x=LinearScale(allow_padding=False, max=1.0, min=0.0), scale_y=LinearScale(allow_padding=False, max=1.0, min=0.0), title=u'Up and Down')" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "\n", "date_col_sc = DateColorScale()\n", "dates = pd.date_range(start='01-01-2007', periods=size)\n", "create_figure(date_col_sc, dates)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "date_col_sc.min = pd.datetime(2007,2,28)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Color Schemes\n", "\n", "Use the following widgets to browse through the available color schemes" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from bqplot.market_map import MarketMap\n", "\n", "from ipywidgets import IntSlider, SelectionSlider, Dropdown\n", "from ipywidgets import VBox, HBox, Layout\n", "from traitlets import link" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Diverging schemes" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "div_schemes = [\n", " 'Spectral',\n", " 'RdYlGn',\n", " 'RdBu',\n", " 'PiYG',\n", " 'PRGn',\n", " 'RdYlBu',\n", " 'BrBG',\n", " 'RdGy',\n", " 'PuOr',\n", "]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c25c6c18fd3843c0be7fc2a0da0a10f5", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type VBox.

\n", "

\n", " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another notebook frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "VBox(children=(HBox(children=(Dropdown(description=u'Scheme', options=('Spectral', 'RdYlGn', 'RdBu', 'PiYG', 'PRGn', 'RdYlBu', 'BrBG', 'RdGy', 'PuOr'), value='Spectral'), SelectionSlider(options=('Spectral', 'RdYlGn', 'RdBu', 'PiYG', 'PRGn', 'RdYlBu', 'BrBG', 'RdGy', 'PuOr'), value='Spectral'))), Figure(axes=[Axis(label_location='end', num_ticks=10, scale=LinearScale()), Axis(orientation='vertical', scale=LinearScale(), side='left'), ColorAxis(orientation='vertical', scale=ColorScale(scheme=u'Spectral'), side='right')], fig_margin={'top': 50, 'right': 100, 'left': 50, 'bottom': 70}, layout=Layout(min_width=u'125px'), marks=[Scatter(color=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]), colors=['steelblue'], interactions={'hover': 'tooltip'}, scales={'y': LinearScale(), 'x': LinearScale(), 'color': ColorScale(scheme=u'Spectral')}, scales_metadata={'opacity': {'dimension': 'opacity'}, 'y': {'orientation': 'vertical', 'dimension': 'y'}, 'color': {'dimension': 'color'}, 'x': {'orientation': 'horizontal', 'dimension': 'x'}, 'skew': {'dimension': 'skew'}, 'rotation': {'dimension': 'rotation'}, 'size': {'dimension': 'size'}}, stroke='black', tooltip_style={'opacity': 0.9}, x=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n", " 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n", " 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), y=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]))], scale_x=LinearScale(allow_padding=False, max=1.0, min=0.0), scale_y=LinearScale(allow_padding=False, max=1.0, min=0.0), title=u'Up and Down')))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def scheme_inspector(fig, schemes, title=''):\n", " '''\n", " Takes a Figure and a list of schemes and returns the Figure along with\n", " sliders to go through the different schemes.\n", " \n", " Supposes that the \n", " '''\n", " \n", " # Get the color scale\n", " col_sc = fig.marks[0].scales['color']\n", " \n", " # Create the widgets to select the colorscheme\n", " scheme_slider = SelectionSlider(description='',\n", " options=schemes)\n", " scheme_dd = Dropdown(description='Scheme',\n", " options=schemes)\n", " link((scheme_dd, 'value'), (scheme_slider, 'value'))\n", "\n", " def update_scheme(*args):\n", " col_sc.scheme = scheme_slider.value\n", " # ax_c.label = scheme_slider.value\n", "\n", " scheme_slider.observe(update_scheme, 'value')\n", " scheme_dd.observe(update_scheme, 'value')\n", " \n", " update_scheme()\n", "\n", " return VBox([HBox([scheme_dd, scheme_slider]), fig])\n", "\n", "scheme_inspector(create_figure(ColorScale(), y_data), div_schemes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Non-diverging schemes" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "lin_schemes = [\n", " 'OrRd',\n", " 'PuBu',\n", " 'BuPu',\n", " 'Oranges',\n", " 'BuGn',\n", " 'YlOrBr',\n", " 'YlGn',\n", " 'Reds',\n", " 'RdPu',\n", " 'Greens',\n", " 'YlGnBu',\n", " 'Purples',\n", " 'GnBu',\n", " 'Greys',\n", " 'YlOrRd',\n", " 'PuRd',\n", " 'Blues',\n", " 'PuBuGn',\n", " 'viridis',\n", " 'plasma',\n", " 'inferno',\n", " 'magma'\n", "]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b25a5129bded4dc88b550f0bca2dff31", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type VBox.

\n", "

\n", " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another notebook frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "VBox(children=(HBox(children=(Dropdown(description=u'Scheme', options=('OrRd', 'PuBu', 'BuPu', 'Oranges', 'BuGn', 'YlOrBr', 'YlGn', 'Reds', 'RdPu', 'Greens', 'YlGnBu', 'Purples', 'GnBu', 'Greys', 'YlOrRd', 'PuRd', 'Blues', 'PuBuGn', 'viridis', 'plasma', 'inferno', 'magma'), value='OrRd'), SelectionSlider(options=('OrRd', 'PuBu', 'BuPu', 'Oranges', 'BuGn', 'YlOrBr', 'YlGn', 'Reds', 'RdPu', 'Greens', 'YlGnBu', 'Purples', 'GnBu', 'Greys', 'YlOrRd', 'PuRd', 'Blues', 'PuBuGn', 'viridis', 'plasma', 'inferno', 'magma'), value='OrRd'))), Figure(axes=[Axis(label_location='end', num_ticks=10, scale=LinearScale()), Axis(orientation='vertical', scale=LinearScale(), side='left'), ColorAxis(orientation='vertical', scale=ColorScale(scheme=u'OrRd'), side='right')], fig_margin={'top': 50, 'right': 100, 'left': 50, 'bottom': 70}, layout=Layout(min_width=u'125px'), marks=[Scatter(color=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]), colors=['steelblue'], interactions={'hover': 'tooltip'}, scales={'y': LinearScale(), 'x': LinearScale(), 'color': ColorScale(scheme=u'OrRd')}, scales_metadata={'opacity': {'dimension': 'opacity'}, 'y': {'orientation': 'vertical', 'dimension': 'y'}, 'color': {'dimension': 'color'}, 'x': {'orientation': 'horizontal', 'dimension': 'x'}, 'skew': {'dimension': 'skew'}, 'rotation': {'dimension': 'rotation'}, 'size': {'dimension': 'size'}}, stroke='black', tooltip_style={'opacity': 0.9}, x=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n", " 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n", " 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), y=array([ 176.4052346 , 216.42095543, 314.29475384, 538.38407376,\n", " 725.13987278, 627.41208479, 722.42092654, 707.28520571,\n", " 696.96332053, 738.02317073, 752.42752784, 897.85487854,\n", " 973.95865106, 986.12615271, 1030.51247598, 1063.87990872,\n", " 1213.28781603, 1192.77198966, 1224.07875982, 1138.66918589,\n", " 883.37020431, 948.73206385, 1035.17568374, 960.9591817 ,\n", " 1187.9346441 , 1042.49807664, 1047.07392837, 1028.35554336,\n", " 1181.6334648 , 1328.56934179, 1344.06408436, 1381.88033632,\n", " 1293.10176156, 1095.02211473, 1060.2308998 , 1075.86579671,\n", " 1198.89486478, 1319.13284966, 1280.40016792, 1250.16989286,\n", " 1145.31459636, 1003.31280264, 832.68578358, 1027.7633231 ,\n", " 976.79810493, 932.99067476, 807.71113876, 885.46017434,\n", " 724.07038959, 702.79636157, 613.24970545, 651.93995523,\n", " 600.85944148, 482.79622306, 479.97800023, 522.81118728,\n", " 529.46290952, 559.71009929, 496.27788993, 460.00377333,\n", " 392.75772855, 356.8024124 , 275.48778419, 102.85952396,\n", " 120.60213818, 80.42404456, -82.59579013, -36.31756458,\n", " -127.04740102, -121.85286144, -48.94380522, -36.04551415,\n", " 77.89455431, -45.58802773, -5.35386361, -73.8348727 ,\n", " -160.91458762, -218.7995541 , -249.95480731, -244.33827309,\n", " -360.85325717, -270.77060847, -224.2043645 , -377.82873313,\n", " -229.00351375, -39.41459614, 78.46336097, 60.47087739,\n", " -46.60438476, 58.84078793, 18.52309324, 140.76760027,\n", " 161.59509808, 259.25900173, 294.89564145, 365.55295827,\n", " 366.60296034, 545.19000973, 557.881219 , 598.08015534]))], scale_x=LinearScale(allow_padding=False, max=1.0, min=0.0), scale_y=LinearScale(allow_padding=False, max=1.0, min=0.0), title=u'Up and Down')))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "scheme_inspector(create_figure(ColorScale(), y_data), lin_schemes,\n", " title='Non-diverging schemes')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OrdinalColorScale\n", "\n", "The `OrdinalColorScale` is a color scale for categorical data, i.e. data that does not have an intrinsic order.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ord_schemes = {\n", " 'Set2': 8,\n", " 'Accent': 8,\n", " 'Set1': 9,\n", " 'Set3': 12,\n", " 'Dark2': 8,\n", " 'Paired': 12,\n", " 'Pastel2': 8,\n", " 'Pastel1': 9,\n", "}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def partition(array, n_groups):\n", " n_elements = len(array)\n", " if n_groups > n_elements:\n", " return np.arange(n_elements)\n", " n_per_group = n_elements // n_groups + (n_elements % n_groups > 0)\n", " return np.tile(range(1, n_groups+1), n_per_group)[:n_elements]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f55ccbb90461421786655cb4cc806525", "version_major": 2, "version_minor": 0 }, "text/html": [ "

Failed to display Jupyter Widget of type VBox.

\n", "

\n", " If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n", " that the widgets JavaScript is still loading. If this message persists, it\n", " likely means that the widgets JavaScript library is either not installed or\n", " not enabled. See the Jupyter\n", " Widgets Documentation for setup instructions.\n", "

\n", "

\n", " If you're reading this message in another notebook frontend (for example, a static\n", " rendering on GitHub or NBViewer),\n", " it may mean that your frontend doesn't currently support widgets.\n", "

\n" ], "text/plain": [ "VBox(children=(HBox(children=(Dropdown(description=u'Scheme', options={'Pastel2': 8, 'Paired': 12, 'Pastel1': 9, 'Set1': 9, 'Set2': 8, 'Set3': 12, 'Dark2': 8, 'Accent': 8}, value=8), SelectionSlider(options={'Pastel2': 8, 'Paired': 12, 'Pastel1': 9, 'Set1': 9, 'Set2': 8, 'Set3': 12, 'Dark2': 8, 'Accent': 8}, value=8), IntSlider(value=3, description=u'n colors', max=8, min=3))), MarketMap(axes=[ColorAxis(label=u'Pastel2', scale=OrdinalColorScale(scheme=u'Pastel2'))], color=array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,\n", " 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,\n", " 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3,\n", " 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,\n", " 3, 1, 2, 3, 1, 2, 3, 1]), colors=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'], display_text=array(['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',\n", " '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',\n", " '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',\n", " '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',\n", " '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',\n", " '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], \n", " dtype='|S1'), groups=array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,\n", " 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1,\n", " 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3,\n", " 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2,\n", " 3, 1, 2, 3, 1, 2, 3, 1]), layout=Layout(min_height=u'600px', min_width=u'800px'), map_margin={'top': 50, 'right': 50, 'left': 50, 'bottom': 50}, names=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n", " 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n", " 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n", " 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n", " 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n", " 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]), scales={'color': OrdinalColorScale(scheme=u'Pastel2')})))" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Define the control widgets\n", "n_groups_slider = IntSlider(description='n colors', min=3)\n", "scheme_slider = SelectionSlider(options=ord_schemes)\n", "scheme_dd = Dropdown(description='Scheme',\n", " options=ord_schemes)\n", "link((scheme_dd, 'label'), (scheme_slider, 'label'))\n", " \n", "def update_scheme(*args):\n", " col_sc.scheme = scheme_slider.label\n", " ax_c.label = scheme_slider.label\n", " n_groups_slider.max = scheme_slider.value\n", "\n", "def update_categories(*args):\n", " groups = partition(names, n_groups_slider.value)\n", " market_map.color = groups\n", " market_map.groups = groups\n", "\n", "n_groups_slider.observe(update_categories, 'value')\n", "scheme_slider.observe(update_scheme, 'value')\n", "\n", "# Define the bqplot marketmap\n", "names = range(100)\n", "\n", "col_sc = OrdinalColorScale()\n", "ax_c = ColorAxis(scale=col_sc)\n", "\n", "market_map = MarketMap(names=names,\n", " display_text=['' for _ in names],\n", " scales={'color': col_sc}, axes=[ax_c],\n", " layout=Layout(min_width='800px', min_height='600px'))\n", "update_scheme()\n", "update_categories()\n", "\n", "VBox([HBox([scheme_dd, scheme_slider, n_groups_slider]), market_map])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.14" } }, "nbformat": 4, "nbformat_minor": 2 }