Bokeh is a Python interactive visualization library that targets modern web browsers for presentation. Its goal is to provide elegant, concise construction of novel graphics in the style of D3.js, and to extend this capability with high-performance interactivity over very large or streaming datasets. Bokeh can help anyone who would like to quickly and easily create interactive plots, dashboards, and data applications.
Here is a simple first example. First we'll import the
figure function from
bokeh.plotting, which will let us create all sorts of interesting plots easily. We also import the
ouptut_notebook functions from
bokeh.io — these let us display our results inline in the notebook.
Next, we'll import NumPy and create some simple data.
Now we'll call Bokeh's
figure function to create a plot
p. Then we call the
circle() method of the plot to render a red circle at each of the points in x and y.
We can immediately interact with the plot:
mousewheel will zoom in and out (after enabling in the toolbar)
The toolbar below is the default one that is available for all plots. It can be configured further via the
tools keyword argument.
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.
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
For each year, we want to plot the distribution of MPG within that year.
This kind of approach can be used to generate other kinds of interesting plots. See many more examples in the Bokeh Documentation Gallery.
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 a "select" tool to select points on one plot, and the linked points on the other plots will highlight.
You can read more about the
ColumnDataSource and other Bokeh data structures in Providing Data for Plots and Tables
In addition to working well with the Notebook, Bokeh can also save plots out into their own HTML files. Here is the bar plot example from above, but saving into its own standalone file.
Now when we call
show(), a new browser tab is also opened with the plot. If we just wanted to save the file, we would use
Bokeh also has a server component that can be used to build interactive web applications that easily connect the powerful constellation of PyData tools to sophisticated Bokeh visualizations. The Bokeh server can be used to:
The cell below shows a simple deployed Bokeh application from https://demo.bokeh.org embedded in an IFrame. Scrub the sliders or change the title to see the plot update.
Click on any of the thumbnails below to launch other live Bokeh applications.
Find more details and information about developing and deploying Bokeh server applications in the User's Guide chapter Running a Bokeh Server.