1What Bokeh is
- A Python library for interactive visualisations that run in a web browser.
- It generates the JavaScript for you. You never write JavaScript.
- Scales from a single plot to a full dashboard, and can handle live streaming data.
- Three output targets: a standalone HTML file, a plot inside a notebook, or a server application.
Chapter 6 placed narratives on a spectrum from author-driven to reader-driven, with the hybrid in between. Bokeh is how you build the hybrid in Python: you fix the structure and the story, and the audience explores inside it and draws its own conclusions.
pip install bokeh # or: conda install bokeh
from bokeh.io import output_notebook
output_notebook() # call ONCE per notebook, enables inline output
Two interfaces
| Interface | Level | Use for |
|---|---|---|
bokeh.plotting | High level, the primary interface | Standard charts. Handles styling and defaults for you |
bokeh.models | Low level | Full control when you need a custom visual |
Work in bokeh.plotting and drop into bokeh.models only when you need a specific component, such as a hover tool or a widget. This is the same trade as low-code against code-based tools from Chapter 5, inside one library.
The core workflow
show(p) is not optional
show(). Unlike Matplotlib in a notebook, nothing renders on its own, and every Bokeh plot ends with show(p).Glyphs
A glyph is the visual mark representing the data. In Bokeh's vocabulary, the line you plot is a glyph, and so is every circle, bar and wedge.
p before calling show, and they all appear on one figure. Common marker names are "circle", "square", "triangle", "asterisk" and "circle_dot".Categorical bar charts, and why x_range matters
x_range= is what tells Bokeh the axis is categorical, not continuous. Without it the axis is treated as numbers and the chart fails. major_label_orientation rotates tick labels when names are long, and since Chapter 3 preferred horizontal text, use it only when abbreviating is not possible.Interactivity you get for free
None of this required any code. That is Bokeh's main argument: the interactivity that Chapter 5 said defines a dashboard is the default rather than something you build.
Clickable legends
click any legend entry above
"hide" removes the series and "mute" fades it instead, which keeps it as context rather than deleting it. This is reader-driven narrative in one line.ColumnDataSource
Bokeh's core data structure, and the thing that makes hover tools and linked plots work.
The hover tool
@column_name refers to a column in the ColumnDataSource, and $index is a Bokeh special field holding the row number. Chapter 4 said bar length supports detection and ranking but weak estimation. A hover tooltip supplies the exact number the eye cannot measure, so hover repairs the known weakness of the encoding. That is the same argument Chapter 5 made about dashboard pop-ups.Widgets
from bokeh.models import DateRangeSlider
date_range_slider = DateRangeSlider(
value=(date(2022, 10, 1), date(2022, 12, 31)),
start=(date(2022, 7, 1)), end=(date(2023, 3, 31)))
A date range slider lets the reader pick a period and see the phenomenon for just that range. It is useful whenever the full series is too long to read at once, for example a financial year you want to view a quarter at a time.
This is audience narrative control: the reader runs their own analysis inside the frame you built.
Layouts
row, column and gridplot
col= and row=, Bokeh needs you to build each plot yourself, and gives you zoom and hover on every panel in return.Decorating the visuals
"green" one of 140 CSS names · "#2e8b57" hex · (0, 100, 100) three-tuple RGB · (100, 100, 100, 0.85) four-tuple RGBA, where the fourth value is alpha from 0 to 1.
Alpha is transparency. Lowering it lets overlapping marks show through, which matters on dense scatter plots.
TEXT text_font_size text_color
LINE line_width line_color line_alpha line_dash
FILL fill_color fill_alpha
HATCH hatch_color hatch_alpha hatch_pattern
The consistency is the point: once you know line_color and fill_alpha, the same names work on every glyph in the library.
p.grid.grid_line_color = None. Because a hover tooltip can supply exact values, you can often drop more furniture here than in a static chart. Bokeh also supports themes, which are the same idea as Matplotlib style sheets from Chapter 7.The three libraries compared
| Matplotlib | Seaborn | Bokeh | |
|---|---|---|---|
| Output | Static image | Static image | Interactive web object |
| Level | Low | High, statistical | High, with low-level models available |
| Strength | Total control | Statistics and defaults handled | Interactivity and browser delivery |
| Best for | Exploration, fine customisation | Statistical charts for reports | Dashboards and shared exploration |
| Data input | Arrays, Series | DataFrame via data= | ColumnDataSource |
Choose by the delivery format. A printed report wants Seaborn. A chart somebody will interrogate wants Bokeh.
Key points
- What Bokeh produces that Matplotlib and Seaborn do not.
- Its three output targets.
- How Bokeh delivers the hybrid narrative from Chapter 6.
output_notebook(), and that it is called once per notebook.bokeh.plottingagainstbokeh.models.- The three-step workflow, and that
show()is mandatory. - What a glyph is, and how multiple glyphs stack on one figure.
- Why
x_range=is needed for a categorical bar chart. - The five default toolbar tools.
legend.click_policy, and what"hide"and"mute"do.- ColumnDataSource: what it maps, and the equal-length rule.
- HoverTool tooltips,
@columnagainst$index, and why hover compensates for weak estimation. - What a widget such as a date range slider gives the reader.
row,columnandgridplot, and how they relate to small multiples.- Four ways to specify a colour, and what alpha controls.
- The four visual property families.
- When to choose Matplotlib, Seaborn or Bokeh.