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

ComponentDescriptionExample Usage
figure()Creates a plot objectp = figure(width=600, height=400)
GlyphsVisual marks (circles, lines, bars)p.circle(x, y), p.line(x, y)
ToolsInteractive controlsPan, Zoom, Reset, Hover, Select
curdoc()Current document containercurdoc().add_root(p)
LayoutsArrange multiple plotscolumn(), row(), gridplot()

Basic Workflow

  1. Create a figure: Define the canvas with tools and properties
  2. Add glyphs: Plot your data using various visual elements
  3. Configure tools: Customize interactivity (hover tooltips, selection)
  4. Style the plot: Set colors, labels, titles, legends
  5. 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:

ToolIconDescriptionKeyboard Shortcut
PanClick and drag to moveHold Shift
Wheel Zoom🔍Scroll to zoomMouse wheel
Box ZoomDraw rectangle to zoom-
ResetReturn 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:

FeatureMatplotlibBokeh
InteractivityStatic by defaultInteractive by default
OutputImages (PNG, SVG)HTML/JavaScript
Use CasePublication figuresWeb dashboards
Learning CurveGentlerSteeper initially
PerformanceGood for staticBetter 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

python
# 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

MethodPurposeExample
figure()Create plotp = figure(width=600, height=400)
p.line()Line plotp.line(x, y, color='blue')
p.circle()Scatter pointsp.circle(x, y, size=10)
p.vbar()Vertical barsp.vbar(x, top=y, width=0.5)
p.hbar()Horizontal barsp.hbar(y, right=x, height=0.5)
column()Stack plots verticallycolumn(p1, p2, p3)
row()Arrange plots horizontallyrow(p1, p2)
HoverTool()Add hover tooltipsp.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.

Maths.pm  ne collecte aucune donnée.
  • Aucun cookie collecté
  • Aucune ligne de log écrite
  • Pas l'ombre d'une base de données distante
  • nihil omnino

  • Ni par pointcarre.app
  • Ni par notre hébergeur
  • Ni par aucun service tiers

Nous expliquons notre démarche zéro donnée conservée sur cette page.

Maths.pm, par

pointcarre.app

Codes sources
Logo licence AGPLv3
Contenus
Logo licence Creative Commons