Created
October 25, 2016 21:09
-
-
Save aiguofer/ad41c3539407d4a8b0c152dc62cd059d to your computer and use it in GitHub Desktop.
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": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"\n", | |
"from bokeh.plotting import figure, show\n", | |
"from bokeh.io import output_notebook, push_notebook\n", | |
"from bokeh.models import ColumnDataSource, CustomJS, DataRange1d, LinearAxis, Range1d\n", | |
"from bokeh.models.widgets import DataTable, TableColumn\n", | |
"\n", | |
"from ipywidgets import interact\n", | |
"\n", | |
"output_notebook()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ix = pd.date_range('2016-01-01', '2017-01-01', freq='d', name='date')\n", | |
"srs = pd.Series(np.random.randint(0, 300, size=len(ix)), index=ix, name='volume')\n", | |
"srs2 = pd.Series(np.random.randint(0, 1000, size=len(ix)), index=ix, name='size')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def get_data(freq='MS'):\n", | |
" return pd.DataFrame(srs.groupby(pd.Grouper(freq=freq)).sum())\n", | |
"\n", | |
"def get_data2(freq='MS'):\n", | |
" return pd.DataFrame(srs2.groupby(pd.Grouper(freq=freq)).sum())\n", | |
"\n", | |
"def get_data3(freq='MS'):\n", | |
" return pd.DataFrame(srs2.groupby(pd.Grouper(freq=freq)).mean())\n", | |
"\n", | |
"source = ColumnDataSource(data=ColumnDataSource.from_df(get_data()))\n", | |
"source2 = ColumnDataSource(data=ColumnDataSource.from_df(get_data2()))\n", | |
"\n", | |
"def get_width():\n", | |
" mindate = min(source.data['date'])\n", | |
" maxdate = max(source.data['date'])\n", | |
" return 0.8 * (maxdate-mindate).total_seconds() * 1000 / len(source.data['date'])\n", | |
"\n", | |
"f = figure(plot_width=550, plot_height=400, x_axis_type=\"datetime\", y_range=Range1d(0, max(source.data['volume']), bounds='auto'))\n", | |
"f.x_range.bounds='auto'\n", | |
"\n", | |
"f.extra_y_ranges = {\n", | |
" \"size\": Range1d(bounds='auto', start=0, end=max(source2.data['size'])),\n", | |
"}\n", | |
"\n", | |
"f.add_layout(LinearAxis(y_range_name='size'), 'right')\n", | |
"r = f.vbar(source=source, top='volume', x='date', width=get_width())\n", | |
"f.line(source=source2, x='date', y='size', color='red', y_range_name='size')\n", | |
"f.tools[2].dimensions = 'width'\n", | |
"\n", | |
"handle = show(f, notebook_handle=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"source3 = ColumnDataSource(data=ColumnDataSource.from_df(get_data2()))\n", | |
"columns = [\n", | |
" TableColumn(field='date'),\n", | |
" TableColumn(field='size')\n", | |
"]\n", | |
"\n", | |
"t = DataTable(source=source3, columns=columns)\n", | |
"t_handle = show(t, notebook_handle=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def update_data(freq={'Quarter': 'QS', 'Month': 'MS', 'Week': 'W'}):\n", | |
" source.data = ColumnDataSource.from_df(get_data(freq))\n", | |
" source2.data = ColumnDataSource.from_df(get_data2(freq))\n", | |
" r.glyph.width = get_width()\n", | |
" f.y_range.end = max(source.data['volume'])\n", | |
" f.extra_y_ranges['size'].end = max(source2.data['size'])\n", | |
"\n", | |
" source3.data = ColumnDataSource.from_df(get_data3(freq))\n", | |
" push_notebook(handle=handle)\n", | |
" push_notebook(handle=t_handle)\n", | |
" \n", | |
"i = interact(update_data)" | |
] | |
} | |
], | |
"metadata": { | |
"hide_input": false, | |
"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.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment