Bokeh 5-minute Overview

Bokeh is an interactive web visualization library for Python (and other languages). It provides d3-like novel graphics, over large datasets, all without requiring any knowledge of Javascript.
It has a Matplotlib compatibility layer, and it works great with the IPython Notebook, but can also be used to generate standalone HTML.

Simple Example

Here is a simple first example. First we'll import the bokeh.plotting module, which defines the graphical functions and primitives.

In [1]:
from bokeh.plotting import *

Next, we'll tell Bokeh to display its plots directly into the notebook. This will cause all of the Javascript and data to be embedded directly into the HTML of the notebook itself. (Bokeh can output straight to HTML files, or use a server, which we'll look at later.)

In [2]:
Bokeh Plot

Configuring embedded BokehJS mode.

Next, we'll import NumPy and create some simple data.

In [3]:
from numpy import *
x = linspace(-6, 6, 100)
y = cos(x)

Now we'll call Bokeh's circle() function to render a red circle at each of the points in x and y.

We can immediately interact with the plot:

  • click-and-drag will pan the plot around.
  • Shift + mousewheel will zoom in and out

(The toolbar is simply a default one that is available for all plots; this can be configured dynamically via the tools keyword argument.)

In [4]:
circle(x, y, color="red", plot_width=500, plot_height=500)
Bokeh Plot

Bar Plot Example

Bokeh's core display model relies on composing graphical primitives which are bound to data series. This is similar in spirit to Protovis and D3, and different than most other Python plotting libraries (except for perhaps Vincent and other, newer libraries).

A slightly more sophisticated example demonstrates this idea.

Bokeh ships with a small set of interesting "sample data" in the bokeh.sampledata package. We'll load up some historical automobile mileage data, which is returned as a Pandas DataFrame.

In [4]:
from bokeh.sampledata.autompg import autompg
grouped = autompg.groupby("yr")
mpg = grouped["mpg"]
avg = mpg.mean()
std = mpg.std()
years = asarray(grouped.groups.keys())
american = autompg[autompg["origin"]==1]
japanese = autompg[autompg["origin"]==3]

For each year, we want to plot the distribution of MPG within that year.

In [5]:
quad(left=years-0.4, right=years+0.4, bottom=avg-std, top=avg+std, 
circle(x=asarray(japanese["yr"]), y=asarray(japanese["mpg"]), 
       alpha=0.4, line_color="red", fill_color=None, line_width=2)
triangle(x=asarray(american["yr"]), y=asarray(american["mpg"]),
         size=8, alpha=0.4, line_color="blue", fill_color=None,
Bokeh Plot

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.

(Click on any of the thumbnails to open the interactive version.)

Linked Brushing

To link plots together at a data level, we can explicitly wrap the data in a ColumnDataSource. This allows us to reference columns by name.

We can use the "select" tool to select points on one plot, and the linked points on the other plots will highlight.

In [6]:
source = ColumnDataSource(autompg.to_dict("list"))
source.add(autompg["yr"], name="yr")
plot_config = dict(plot_width=400, plot_height=400, tools="pan,wheel_zoom,box_zoom,select")
  circle("yr", "mpg", color="blue", title="MPG by Year",
         source=source, **plot_config),
  circle("hp", "displ", color="green", title="HP vs. Displacement",
         source=source, **plot_config),
  circle("mpg", "displ", size="cyl", line_color="red", title="MPG vs. Displacement",
         fill_color=None, source=source, **plot_config)
Bokeh Plot