Last active
September 22, 2018 15:13
-
-
Save jmatt/a141de633a5c3869b495038f1bc21b6b to your computer and use it in GitHub Desktop.
Holoviews Bokeh crossfilter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import os\n", | |
"import urllib\n", | |
"\n", | |
"import holoviews as hv\n", | |
"\n", | |
"from bokeh.resources import INLINE\n", | |
"from bokeh.io import output_notebook\n", | |
"from bokeh.layouts import row, widgetbox\n", | |
"from bokeh.models import Select\n", | |
"from bokeh.plotting import curdoc\n", | |
"from bokeh.sampledata.autompg import autompg\n", | |
"from bokeh.io import show\n", | |
"\n", | |
"\n", | |
"output_notebook(INLINE, hide_banner=True)\n", | |
"\n", | |
"\n", | |
"def jupyter_proxy_url(notebook_url, port):\n", | |
" base_url = os.environ['EXTERNAL_URL']\n", | |
" service_url_path = os.environ['JUPYTERHUB_SERVICE_PREFIX']\n", | |
" proxy_url_path = 'proxy/%d' % port\n", | |
" user_url = urllib.parse.urljoin(base_url, service_url_path)\n", | |
" full_url = urllib.parse.urljoin(user_url, proxy_url_path)\n", | |
" return full_url\n", | |
"\n", | |
"\n", | |
"def crossfilter(doc):\n", | |
" df = autompg.copy()\n", | |
"\n", | |
" SIZES = list(range(6, 22, 3))\n", | |
" ORIGINS = ['North America', 'Europe', 'Asia']\n", | |
"\n", | |
" # data cleanup\n", | |
" df.cyl = [str(x) for x in df.cyl]\n", | |
" df.origin = [ORIGINS[x-1] for x in df.origin]\n", | |
"\n", | |
" df['year'] = [str(x) for x in df.yr]\n", | |
" del df['yr']\n", | |
"\n", | |
" df['mfr'] = [x.split()[0] for x in df.name]\n", | |
" df.loc[df.mfr=='chevy', 'mfr'] = 'chevrolet'\n", | |
" df.loc[df.mfr=='chevroelt', 'mfr'] = 'chevrolet'\n", | |
" df.loc[df.mfr=='maxda', 'mfr'] = 'mazda'\n", | |
" df.loc[df.mfr=='mercedes-benz', 'mfr'] = 'mercedes'\n", | |
" df.loc[df.mfr=='toyouta', 'mfr'] = 'toyota'\n", | |
" df.loc[df.mfr=='vokswagen', 'mfr'] = 'volkswagen'\n", | |
" df.loc[df.mfr=='vw', 'mfr'] = 'volkswagen'\n", | |
" del df['name']\n", | |
"\n", | |
" columns = sorted(df.columns)\n", | |
" discrete = [x for x in columns if df[x].dtype == object]\n", | |
" continuous = [x for x in columns if x not in discrete]\n", | |
" quantileable = [x for x in continuous if len(df[x].unique()) > 20]\n", | |
"\n", | |
" renderer = hv.renderer('bokeh')\n", | |
" options = hv.Store.options(backend='bokeh')\n", | |
" options.Points = hv.Options('plot', width=800, height=600, size_index=None,)\n", | |
" options.Points = hv.Options('style', cmap='rainbow', line_color='black')\n", | |
"\n", | |
" def create_figure():\n", | |
" label = \"%s vs %s\" % (x.value.title(), y.value.title())\n", | |
" kdims = [x.value, y.value]\n", | |
"\n", | |
" opts, style = {}, {}\n", | |
" opts['color_index'] = color.value if color.value != 'None' else None\n", | |
" if size.value != 'None':\n", | |
" opts['size_index'] = size.value\n", | |
" opts['scaling_factor'] = (1./df[size.value].max())*200\n", | |
" points = hv.Points(df, kdims=kdims, label=label).opts(plot=opts, style=style)\n", | |
" return renderer.get_plot(points).state\n", | |
"\n", | |
" def update(attr, old, new):\n", | |
" layout.children[1] = create_figure()\n", | |
"\n", | |
" x = Select(title='X-Axis', value='mpg', options=quantileable)\n", | |
" x.on_change('value', update)\n", | |
"\n", | |
" y = Select(title='Y-Axis', value='hp', options=quantileable)\n", | |
" y.on_change('value', update)\n", | |
"\n", | |
" size = Select(title='Size', value='None', options=['None'] + quantileable)\n", | |
" size.on_change('value', update)\n", | |
"\n", | |
" color = Select(title='Color', value='None', options=['None'] + quantileable)\n", | |
" color.on_change('value', update)\n", | |
"\n", | |
" controls = widgetbox([x, y, color, size], width=200)\n", | |
" layout = row(controls, create_figure())\n", | |
"\n", | |
" doc.add_root(layout)\n", | |
" doc.title = \"Crossfilter\"\n", | |
" \n", | |
"show(crossfilter, proxy_url_func=jupyter_proxy_url)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment