Bokeh is an interactive web visualization library for Python
(and other languages). It provides d3-like novel graphics, over
It has a Matplotlib compatibility layer, and it works great with the IPython Notebook, but can also be used to generate standalone HTML.
Here is a simple first example. First we'll import the
module, which defines the graphical functions and primitives.
from bokeh.plotting import *
Configuring embedded BokehJS mode.
Next, we'll import NumPy and create some simple data.
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:
(The toolbar is simply a default one that is available for all plots;
this can be configured dynamically via the
tools keyword argument.)
circle(x, y, color="red", plot_width=500, plot_height=500) show()
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
package. We'll load up some historical automobile mileage data, which is returned
as a Pandas
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.
hold(True) figure() quad(left=years-0.4, right=years+0.4, bottom=avg-std, top=avg+std, fill_alpha=0.4) circle(x=asarray(japanese["yr"]), y=asarray(japanese["mpg"]), size=8, 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, line_width=2) hold(False) show()
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.)
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.
output_notebook() 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") gridplot([[ 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) ]]) show()