What it is
Bokeh is an interactive visualization library for Python that targets modern web browsers for presentation. It allows the creation of interactive plots, dashboards, and data applications with high-performance interactivity over large datasets.
Bokeh provides a Python interface to generate interactive plots that render in browsers using HTML and JavaScript. It supports line plots, scatter plots, bar charts, heatmaps, widgets, and server-side apps with real-time interactivity.
Installation
pip install bokehGetting started
The smallest useful thing you can do with it, and what each part means.
from bokeh.plotting import figure, show
p = figure(title='Simple Line Plot', x_axis_label='x', y_axis_label='y')
p.line([1,2,3,4,5], [6,7,2,4,5], line_width=2)
show(p)from bokeh.plotting import figure, show
p = figure(title='Scatter Plot', x_axis_label='x', y_axis_label='y')
p.circle([1,2,3,4], [4,7,2,5], size=10, color='navy', alpha=0.5)
show(p)Advanced usage
Where the library earns its place over a simpler alternative.
from bokeh.models import HoverTool
hover = HoverTool(tooltips=[('x','@x'),('y','@y')])
p.add_tools(hover)from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show
source = ColumnDataSource(data=dict(fruits=['Apple','Banana','Orange'], counts=[10,20,15]))
p = figure(x_range=source.data['fruits'], title='Fruit Counts')
p.vbar(x='fruits', top='counts', width=0.9, source=source)
show(p)from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
source = ColumnDataSource(data=dict(x=[1,2,3], y=[4,5,6]))
p = figure()
p.line('x','y', source=source)
curdoc().add_root(p)from bokeh.layouts import row
p1 = figure()
p1.circle([1,2,3],[4,5,6])
p2 = figure(x_range=p1.x_range)
p2.line([1,2,3],[6,5,4])
show(row(p1,p2))Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- ValueError: mismatched data lengths
- Ensure that all lists or arrays passed to Bokeh glyphs have the same length.
- ImportError: No module named 'bokeh'
- Install Bokeh using pip or conda in your Python environment.
- RuntimeError: Bokeh server not running
- Start the Bokeh server using `bokeh serve --show script.py` for interactive apps.
Best practices
- Use ColumnDataSource for better performance and easier interactivity.
- Leverage Bokeh server for live-updating dashboards.
- Keep visualizations clean and label axes and legends for clarity.
- Combine with Pandas for easy data handling and plotting.
- Profile complex plots for performance with large datasets.
Background
Why it exists, and what it was reacting to.
Bokeh was created by Bryan Van de Ven and Continuum Analytics (now Anaconda Inc.) in 2013 to enable Python users to build interactive, browser-based visualizations without needing to write JavaScript. Its goal is to provide elegant, concise construction of versatile graphics and dashboards suitable for web presentation.
