Bokeh

Language: Python

Data Science

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.

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.

Installation

pip: pip install bokeh
conda: conda install -c bokeh bokeh

Usage

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.

Simple line plot

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)

Creates a simple line plot with labeled axes and displays it in a browser.

Scatter plot

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)

Generates a scatter plot with circle markers, color, and transparency settings.

Adding Hover Tooltips

from bokeh.models import HoverTool
hover = HoverTool(tooltips=[('x','@x'),('y','@y')])
p.add_tools(hover)

Adds interactive hover tooltips that display x and y values when the user hovers over points.

Creating a Bar Chart

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)

Creates a vertical bar chart from a ColumnDataSource.

Interactive Bokeh Server App

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)

Demonstrates creating a Bokeh app that can be served with `bokeh serve` for real-time interactivity.

Linking multiple plots

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))

Links x-axis range between two plots to allow synchronized zooming and panning.

Error Handling

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.