Bokeh exampleΒΆ

This example demonstrates the use of bokeh in the sphinx-nbgallery.

The example is taken from the bokeh-notebook gallery to demonstrate the use bokeh. For the sphinx-nbgallery, you have to do 3 additional modifications in the example_gallery_config of your

  1. You have to set the insert_bokeh configuration value because we need additional style sheets and javascript files
  2. Note, that, since the output_notebook function needs some time, we recommend to use the dont_preprocess configuration value for this notebook.
  3. We cannot estimate a thumbnail figure for a notebook not using matplotlib. So you should provide it using the thumbnail_figure metadata key (as it has been done for this notebook)
from bokeh.models import HoverTool, ColumnDataSource
from bokeh.palettes import Viridis6
from bokeh.plotting import figure, show, output_notebook
from bokeh.sampledata.us_counties import data as counties
from bokeh.sampledata.unemployment import data as unemployment
counties = {
    code: county for code, county in counties.items() if county["state"] == "tx"

county_xs = [county["lons"] for county in counties.values()]
county_ys = [county["lats"] for county in counties.values()]
county_names = [county['name'] for county in counties.values()]
county_rates = [unemployment[county_id] for county_id in counties]
county_colors = [Viridis6[int(rate/3)] for rate in county_rates]

source = ColumnDataSource(data=dict(
Loading BokehJS ...

p = figure(title="Texas Unemployment 2009", tools=TOOLS,
           x_axis_location=None, y_axis_location=None)
p.grid.grid_line_color = None

p.patches('x', 'y', source=source,
          fill_color='color', fill_alpha=0.7,
          line_color="white", line_width=0.5)

hover = p.select_one(HoverTool)
hover.point_policy = "follow_mouse"
hover.tooltips = [
    ("Name", "@name"),
    ("Unemployment rate)", "@rate%"),
    ("(Long, Lat)", "($x, $y)"),

<Bokeh Notebook handle for In[6]>

Download python script:

Download Jupyter notebook: example_bokeh.ipynb

View the notebook in the Jupyter nbviewer

Run this example interactively: binder