Files
2024-05-01 12:28:44 -06:00

3.2 KiB

A very useful Python library, particularly for creating interactive visualizations in web browsers, is Bokeh. Bokeh enables the building of complex statistical plots quickly and through simple commands. It's highly versatile, allowing for the creation of interactive plots, dashboards, and data applications with rich, web-based visualization capabilities. Below is a concise reference guide for common use cases with Bokeh, formatted in Markdown syntax:

Bokeh Reference Guide

Installation

pip install bokeh

Basic Plotting

Importing Bokeh

from bokeh.plotting import figure, show, output_file

Creating a Simple Line Plot

# Prepare some data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

# Output to static HTML file (opens in browser)
output_file("lines.html")

# Create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

# Add a line renderer with legend and line thickness
p.line(x, y, legend_label="Temp.", line_width=2)

# Show the results
show(p)

Interactive Plots

Adding Tools

from bokeh.models import PanTool, ResetTool, HoverTool

# Adding pan, reset functionality
p.add_tools(PanTool(), ResetTool())

# Adding hover tool
hover = HoverTool()
hover.tooltips=[
    ("Index", "$index"),
    ("(x,y)", "($x, $y)"),
]
p.add_tools(hover)

Widgets and Interactivity

from bokeh.layouts import column
from bokeh.models import Slider, Button
from bokeh.io import curdoc

# Create some widgets
slider = Slider(start=0, end=10, value=1, step=.1, title="Stuff")
button = Button(label="Press me")

# Update function
def update():
    # Update function for widgets
    ...

# Button click event
button.on_click(update)

# Arrange widgets and plot into a layout
layout = column(slider, button, p)

# Add the layout to the current document
curdoc().add_root(layout)

Embedding and Linking Plots

Linking Plots Together

from bokeh.layouts import gridplot

# Linking axes
p1 = figure(width=250, height=250)
p2 = figure(width=250, height=250, x_range=p1.x_range, y_range=p1.y_range)
p3 = figure(width=250, height=250, x_range=p1.x_range)

# Arrange plots in a grid
grid = gridplot([[p1, p2], [None, p3]])

show(grid)

Embedding Bokeh Plots in HTML

from bokeh.embed import components

script, div = components(p)

# `script` is a <script> tag that contains the data for your plot
# `div` is a <div> tag that the plot view is loaded into

Server Applications

Running a Bokeh Server

# Save your script as, e.g., `myapp.py`
# Run the Bokeh server
bokeh serve --show myapp.py
# Inside myapp.py, use curdoc to add your layout, figures, etc.
from bokeh.io import curdoc
# Define plots and widgets here
curdoc().add_root(layout)

Bokeh excels in creating interactive and visually appealing plots that can be easily embedded into web pages or displayed as part of a Python notebook. Its capability to handle large, dynamic data sets makes it suitable for real-time data applications, and its interactivity features engage users in exploring the data more deeply. This guide introduces the basics, but Bokeh's comprehensive features enable much more complex data visualization and interactive applications.