{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# Bokeh 5-minute Overview\n", "\n", "Bokeh is an interactive web visualization library for Python \n", "(and other languages). It provides d3-like novel graphics, over\n", "large datasets, all without requiring any knowledge of Javascript. It \n", "has a Matplotlib compatibility layer, and it works great with\n", "the IPython Notebook, but can also be used to generate standalone HTML.\n", "\n", "## Simple Example\n", "\n", "Here is a simple first example. First we'll import the `bokeh.plotting`\n", "module, which defines the graphical functions and primitives." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.plotting import figure, output_notebook, show" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we'll tell Bokeh to display its plots directly into the notebook.\n", "This will cause all of the Javascript and data to be embedded directly\n", "into the HTML of the notebook itself.\n", "(Bokeh can output straight to HTML files, or use a server, which we'll\n", "look at later.)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(global) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " if (typeof (window._bokeh_onload_callbacks) === \"undefined\") {\n", " window._bokeh_onload_callbacks = [];\n", " }\n", "\n", " function run_callbacks() {\n", " window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " delete window._bokeh_onload_callbacks\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " window._bokeh_onload_callbacks.push(callback);\n", " if (window._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " window._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " window._bokeh_is_loading--;\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };\n", "\n", " var js_urls = ['https://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-compiler-0.12.0.min.js'];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " Bokeh.$(\"#d872bc72-1b16-466e-9c77-322df752d8fd\").text(\"BokehJS successfully loaded\");\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i](window.Bokeh);\n", " }\n", " }\n", "\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(this));" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we'll import NumPy and create some simple data." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from numpy import cos, linspace\n", "x = linspace(-6, 6, 100)\n", "y = cos(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we'll call Bokeh's `circle()` function to render a red circle at\n", "each of the points in x and y.\n", "\n", "We can immediately interact with the plot:\n", "\n", " * click-drag will pan the plot around.\n", " * mousewheel will zoom in and out\n", " \n", "(The toolbar is simply a default one that is available for all plots;\n", "this can be configured dynamically via the `tools` keyword argument.)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

<Bokeh Notebook handle for In[4]>

" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = figure(width=500, height=500)\n", "p.circle(x, y, size=7, color=\"firebrick\", alpha=0.5)\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Bar Plot Example\n", "\n", "\n", "Bokeh's core display model relies on *composing graphical primitives* which\n", "are bound to data series. This is similar in spirit to Protovis and D3,\n", "and different than most other Python plotting libraries (except for perhaps\n", "Vincent and other, newer libraries).\n", "\n", "A slightly more sophisticated example demonstrates this idea.\n", "\n", "Bokeh ships with a small set of interesting \"sample data\" in the `bokeh.sampledata`\n", "package. We'll load up some historical automobile mileage data, which is returned\n", "as a Pandas `DataFrame`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from bokeh.sampledata.autompg import autompg\n", "from numpy import array\n", "\n", "grouped = autompg.groupby(\"yr\")\n", "mpg = grouped[\"mpg\"]\n", "avg = mpg.mean()\n", "std = mpg.std()\n", "years = array(list(grouped.groups.keys()))\n", "american = autompg[autompg[\"origin\"]==1]\n", "japanese = autompg[autompg[\"origin\"]==3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each year, we want to plot the distribution of MPG within that year." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

<Bokeh Notebook handle for In[6]>

" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = figure()\n", "\n", "p.quad(left=years-0.4, right=years+0.4, bottom=avg-std, top=avg+std, fill_alpha=0.4)\n", "\n", "p.circle(x=japanese[\"yr\"], y=japanese[\"mpg\"], size=8,\n", " alpha=0.4, line_color=\"red\", fill_color=None, line_width=2)\n", "\n", "p.triangle(x=american[\"yr\"], y=american[\"mpg\"], size=8, \n", " alpha=0.4, line_color=\"blue\", fill_color=None, line_width=2)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# This kind of approach can be used to generate other kinds of interesting plots, like some of the following which are available on the [Bokeh web page](http://bokeh.pydata.org/en/latest). \n", "\n", "*(Click on any of the thumbnails to open the interactive version.)*\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linked Brushing\n", "\n", "To link plots together at a data level, we can explicitly wrap the data in a ColumnDataSource.\n", "This allows us to reference columns by name.\n", "\n", "We can use the \"select\" tool to select points on one plot, and the linked points\n", "on the other plots will highlight." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

<Bokeh Notebook handle for In[7]>

" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.models import ColumnDataSource\n", "from bokeh.layouts import gridplot\n", "\n", "source = ColumnDataSource(autompg.to_dict(\"list\"))\n", "source.add(autompg[\"yr\"], name=\"yr\")\n", "\n", "plot_config = dict(plot_width=300, plot_height=300,\n", " tools=\"pan,wheel_zoom,box_zoom,box_select,lasso_select\")\n", "\n", "p1 = figure(title=\"MPG by Year\", **plot_config)\n", "p1.circle(\"yr\", \"mpg\", color=\"blue\", source=source)\n", "\n", "p2 = figure(title=\"HP vs. Displacement\", **plot_config)\n", "p2.circle(\"hp\", \"displ\", color=\"green\", source=source)\n", "\n", "p3 = figure(title=\"MPG vs. Displacement\", **plot_config)\n", "p3.circle(\"mpg\", \"displ\", size=\"cyl\", line_color=\"red\", fill_color=None, source=source)\n", "\n", "p = gridplot([[ p1, p2, p3]], toolbar_location=\"right\")\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Standalone HTML\n", "\n", "In addition to working well with the Notebook, Bokeh can also\n", "save plots out into their own HTML files. Here is the bar plot\n", "example from above, but saving into its own standalone file.\n", "\n", "Note that when we call `show()`, a new browser tab is opened.\n", "(If we just wanted to save the file, we would use `save()` instead.)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "

<Bokeh Notebook handle for In[8]>

" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from bokeh.plotting import output_file\n", "\n", "output_file(\"barplot.html\")\n", "\n", "p = figure()\n", "\n", "p.quad(left=years-0.4, right=years+0.4, bottom=avg-std, top=avg+std, fill_alpha=0.4)\n", "\n", "p.circle(x=japanese[\"yr\"], y=japanese[\"mpg\"], size=8, \n", " alpha=0.4, line_color=\"red\", fill_color=None, line_width=2)\n", "\n", "p.triangle(x=american[\"yr\"], y=american[\"mpg\"], size=8, \n", " alpha=0.4, line_color=\"blue\", fill_color=None, line_width=2)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Bokeh Apps\n", "\n", "When the linked brushing and server-based operation are combined,\n", "you can build graphical \"applets\", which resemble things like\n", "what Crossfilter and others do. However, Bokeh provides the\n", "reactive object model across client and server, so these sorts\n", "of selections and interactions can trigger server-side code,\n", "which is implemented in Python.\n", "\n", "*(Click to launch the live app.)*\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## BokehJS\n", "\n", "At its core, Bokeh consists of a Javascript library, [BokehJS](https://github.com/bokeh/bokeh/tree/master/bokehjs), and a Python binding which provides classes and objects that ultimately generate a JSON representation of the plot structure.\n", "\n", "You can read more about design and usage in the [Developing with JavaScript](http://bokeh.pydata.org/en/latest/docs/user_guide/bokehjs.html) section of the Bokeh User's Guide." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More Information\n", "----------------\n", "\n", "Full documentation and live examples: http://bokeh.pydata.org/en/latest\n", "\n", "GitHub: https://github.com/bokeh/bokeh\n", "\n", "Mailing list: [bokeh@continuum.io](mailto:bokeh@continuum.io)\n", "\n", "Gitter: https://gitter.im/bokeh/bokeh\n", "\n", "Be sure to follow us on Twitter [@bokehplots](http://twitter.com/BokehPlots>), as well as on [Youtube](https://www.youtube.com/channel/UCK0rSk29mmg4UT4bIOvPYhw) and [Vine](https://vine.co/bokehplots)!\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }