Created
June 27, 2017 17:48
-
-
Save jakevdp/0234f96b2a9ae4a28d606bf45ea53393 to your computer and use it in GitHub Desktop.
Simple DataShader interactive demo with no external datasets
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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"*Installation*\n", | |
"\n", | |
"```\n", | |
"conda install -c bokeh datashader\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import datashader as ds\n", | |
"import datashader.transfer_functions as tf\n", | |
"import pandas as pd\n", | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = np.random.randn(10000000)\n", | |
"y = np.sin(5 * x) + np.cos(6 * x) + 0.1 * np.random.randn(len(x))\n", | |
"z = x ** 2 + y ** 2\n", | |
"df = pd.DataFrame({'x': x, 'y': y, 'z':z})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cvs = ds.Canvas(plot_width=400, plot_height=400)\n", | |
"agg = cvs.points(df, 'x', 'y')#, ds.mean('z'))\n", | |
"tf.shade(agg, cmap=['lightblue', 'darkblue'], how='log')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from bokeh.models import BoxZoomTool\n", | |
"from bokeh.plotting import figure, output_notebook, show\n", | |
"\n", | |
"output_notebook()\n", | |
"\n", | |
"x_range = (-5, 5)\n", | |
"y_range = (-5, 5)\n", | |
"\n", | |
"plot_width = int(750)\n", | |
"plot_height = int(plot_width//1.2)\n", | |
"\n", | |
"def base_plot(tools='pan,wheel_zoom,reset',plot_width=plot_width, plot_height=plot_height, **plot_args):\n", | |
" p = figure(tools=tools, plot_width=plot_width, plot_height=plot_height,\n", | |
" x_range=x_range, y_range=y_range, outline_line_color=None,\n", | |
" min_border=0, min_border_left=0, min_border_right=0,\n", | |
" min_border_top=0, min_border_bottom=0, **plot_args)\n", | |
" \n", | |
" p.axis.visible = False\n", | |
" p.xgrid.grid_line_color = None\n", | |
" p.ygrid.grid_line_color = None\n", | |
" \n", | |
" p.add_tools(BoxZoomTool(match_aspect=True))\n", | |
" \n", | |
" return p\n", | |
" \n", | |
"options = dict(line_color=None, fill_color='blue', size=5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"samples = df.sample(n=1000)\n", | |
"p = base_plot()\n", | |
"p.circle(x=samples['x'], y=samples['y'], **options)\n", | |
"show(p)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import datashader as ds\n", | |
"from datashader import transfer_functions as tf\n", | |
"from datashader.colors import Greys9\n", | |
"Greys9_r = list(reversed(Greys9))[:-2]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"cvs = ds.Canvas(plot_width=plot_width, plot_height=plot_height, x_range=x_range, y_range=y_range)\n", | |
"agg = cvs.points(df, 'x', 'y', ds.count('z'))\n", | |
"tf.shade(agg, cmap=[\"white\", 'darkblue'], how='linear')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import datashader as ds\n", | |
"from datashader.bokeh_ext import InteractiveImage\n", | |
"from functools import partial\n", | |
"from datashader.utils import export_image\n", | |
"from datashader.colors import colormap_select, Greys9, Hot, viridis, inferno\n", | |
"from IPython.core.display import HTML, display\n", | |
"\n", | |
"background = \"black\"\n", | |
"export = partial(export_image, export_path=\"export\", background=background)\n", | |
"cm = partial(colormap_select, reverse=(background==\"black\"))\n", | |
"\n", | |
"def create_image(x_range, y_range, w=plot_width, h=plot_height):\n", | |
" cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range)\n", | |
" agg = cvs.points(df, 'x', 'y', ds.count('z'))\n", | |
" img = tf.shade(agg, cmap=Hot, how='eq_hist')\n", | |
" return tf.dynspread(img, threshold=0.5, max_px=4)\n", | |
"\n", | |
"p = base_plot(background_fill_color=background)\n", | |
"export(create_image(x_range, y_range),\"NYCT_hot\")\n", | |
"InteractiveImage(p, create_image)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.6 + datashader", | |
"language": "python", | |
"name": "datashader" | |
}, | |
"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.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment