Bokeh presentation and basic usage
Bokeh enables the creation of interactive visualizations for data analysis and storytelling.
Introduction to Bokeh
What is Bokeh?
Bokeh is a Python library for creating interactive visualizations for modern web browsers. Unlike static plots from matplotlib, Bokeh plots allow users to pan, zoom, select, and hover over data points.
Key advantages of Bokeh:
- Interactive by default: Pan, zoom, reset, save tools built-in
- Web-ready: Outputs HTML/JavaScript that works in any browser
- Server capabilities: Can build interactive dashboards and applications
- Large dataset handling: Efficient rendering of millions of points
Core Concepts
Key Components
Component | Description | Example Usage |
---|---|---|
figure() | Creates a plot object | p = figure(width=600, height=400) |
Glyphs | Visual marks (circles, lines, bars) | p.circle(x, y) , p.line(x, y) |
Tools | Interactive controls | Pan, Zoom, Reset, Hover, Select |
curdoc() | Current document container | curdoc().add_root(p) |
Layouts | Arrange multiple plots | column() , row() , gridplot() |
Basic Workflow
- Create a figure: Define the canvas with tools and properties
- Add glyphs: Plot your data using various visual elements
- Configure tools: Customize interactivity (hover tooltips, selection)
- Style the plot: Set colors, labels, titles, legends
- Add to document: Make the plot available for rendering
Getting Started with Bokeh
2.1. Your First Interactive Plot
2.2. Understanding Bokeh Tools
Built-in tools provide interactivity without additional code:
Tool | Icon | Description | Keyboard Shortcut |
---|---|---|---|
Pan | ✋ | Click and drag to move | Hold Shift |
Wheel Zoom | 🔍 | Scroll to zoom | Mouse wheel |
Box Zoom | ⬜ | Draw rectangle to zoom | - |
Reset | ↺ | Return to original view | - |
Save | 💾 | Download plot as PNG | - |
Hover | ℹ️ | Show data on mouse over | - |
Real Data Visualizations with GDP Dataset
3.1. Loading and Preparing Data
3.2. Interactive Time Series - GDP Evolution
Advanced Interactive Features
4.1. Scatter Plot with Size and Color Mapping
4.2. Interactive Bar Chart with Sorting
Combining Multiple Plots
5.1. Dashboard Layout with Linked Plots
The data below is randomly generated.
5.2. Mathematical Functions Explorer
Interactive Widgets and Controls
There may be problems with the update of the plot when using the widgets in the browser. Use the .py
files available at folder.
6.1. Top 15 Economies Comparison (2019 Ranking)
6.2. Comparative GDP Growth Analysis
6.3. Interactive Year Slider Visualization
Summary and Best Practices
Key Takeaways
Bokeh vs Matplotlib Comparison:
Feature | Matplotlib | Bokeh |
---|---|---|
Interactivity | Static by default | Interactive by default |
Output | Images (PNG, SVG) | HTML/JavaScript |
Use Case | Publication figures | Web dashboards |
Learning Curve | Gentler | Steeper initially |
Performance | Good for static | Better for large datasets |
When to Use Bokeh
✅ Use Bokeh when you need: - Interactive exploration of data - Web-based visualizations - Real-time data updates - Linked plots and dashboards - Hover tooltips and selection tools
❌ Consider alternatives when: - Creating static publication figures (use Matplotlib) - Need 3D visualizations (use Plotly) - Simple quick plots (use Matplotlib) - Working offline without web output
Essential Bokeh Patterns
# 1. Always use curdoc() for Pyodide/JupyterLite
from bokeh.plotting import curdoc
curdoc().add_root(plot)
# 2. Configure hover tooltips for better UX
hover = HoverTool(tooltips=[("Label", "@field")])
p.add_tools(hover)
# 3. Enable legend interaction
p.legend.click_policy = "hide"
# 4. Use ColumnDataSource for complex data
from bokeh.models import ColumnDataSource
source = ColumnDataSource(dataframe)
Quick Reference
Method | Purpose | Example |
---|---|---|
figure() | Create plot | p = figure(width=600, height=400) |
p.line() | Line plot | p.line(x, y, color='blue') |
p.circle() | Scatter points | p.circle(x, y, size=10) |
p.vbar() | Vertical bars | p.vbar(x, top=y, width=0.5) |
p.hbar() | Horizontal bars | p.hbar(y, right=x, height=0.5) |
column() | Stack plots vertically | column(p1, p2, p3) |
row() | Arrange plots horizontally | row(p1, p2) |
HoverTool() | Add hover tooltips | p.add_tools(HoverTool()) |
Remember: Bokeh excels at creating interactive, web-ready visualizations that allow users to explore data dynamically. While it requires more setup than matplotlib for simple plots, it provides unmatched interactivity for data exploration and presentation.