{ "metadata": { "name": "", "signature": "sha256:9afb7e31d75c9582e349ec5fba1fcf7be8b875bc32ebef4dc86f657bf02f5515" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Bokeh 5-minute Overview\n", "

\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. \n", "It 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", "\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", "collapsed": false, "input": [ "from bokeh.plotting import *" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "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", "collapsed": false, "input": [ "output_notebook()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", " \n", " \n", " Bokeh Plot\n", " \n", " \n", " \n", " \n", " \n", "

Configuring embedded BokehJS mode.

\n", " \n", "" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, we'll import NumPy and create some simple data." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import *\n", "x = linspace(-6, 6, 100)\n", "y = cos(x)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "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-and-drag will pan the plot around.\n", " * Shift + 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", "collapsed": false, "input": [ "circle(x, y, color=\"red\", plot_width=500, plot_height=500)\n", "show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", " \n", " \n", " Bokeh Plot\n", " \n", " \n", " \n", "
Plots
\n", " \n", "" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 4 }, { "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", "collapsed": false, "input": [ "from bokeh.sampledata.autompg import autompg\n", "grouped = autompg.groupby(\"yr\")\n", "mpg = grouped[\"mpg\"]\n", "avg = mpg.mean()\n", "std = mpg.std()\n", "years = asarray(grouped.groups.keys())\n", "american = autompg[autompg[\"origin\"]==1]\n", "japanese = autompg[autompg[\"origin\"]==3]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each year, we want to plot the distribution of MPG within that year." ] }, { "cell_type": "code", "collapsed": true, "input": [ "hold(True)\n", "figure()\n", "quad(left=years-0.4, right=years+0.4, bottom=avg-std, top=avg+std, \n", " fill_alpha=0.4)\n", "circle(x=asarray(japanese[\"yr\"]), y=asarray(japanese[\"mpg\"]), \n", " size=8,\n", " alpha=0.4, line_color=\"red\", fill_color=None, line_width=2)\n", "triangle(x=asarray(american[\"yr\"]), y=asarray(american[\"mpg\"]),\n", " size=8, alpha=0.4, line_color=\"blue\", fill_color=None,\n", " line_width=2)\n", "hold(False)\n", "show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", " \n", " \n", " Bokeh Plot\n", " \n", " \n", " \n", "
Plots
\n", " \n", "" ], "metadata": {}, "output_type": "display_data" } ], "prompt_number": 5 }, { "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). \n", "\n", "*(Click on any of the thumbnails to open the interactive version.)*\n", "\n", "\n", "\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Linked Brushing\n", "---------------\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", "collapsed": false, "input": [ "output_notebook()\n", "source = ColumnDataSource(autompg.to_dict(\"list\"))\n", "source.add(autompg[\"yr\"], name=\"yr\")\n", "plot_config = dict(plot_width=400, plot_height=400, tools=\"pan,wheel_zoom,box_zoom,select\")\n", "gridplot([[\n", " circle(\"yr\", \"mpg\", color=\"blue\", title=\"MPG by Year\",\n", " source=source, **plot_config),\n", " circle(\"hp\", \"displ\", color=\"green\", title=\"HP vs. Displacement\",\n", " source=source, **plot_config),\n", " circle(\"mpg\", \"displ\", size=\"cyl\", line_color=\"red\", title=\"MPG vs. Displacement\",\n", " fill_color=None, source=source, **plot_config)\n", " ]])\n", "show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", " \n", " \n", " Bokeh Plot\n", " \n", "