Last modified: Jan 01, 2025 By Alexander Williams

How to Install and Get Started with Python Bokeh

Bokeh is a powerful Python library for creating interactive visualizations that can be displayed in modern web browsers. This guide will walk you through the installation process and basic usage.

Installing Bokeh

The easiest way to install Bokeh is using pip, Python's package installer. Open your terminal or command prompt and run:


pip install bokeh

To verify the installation, run Python and import Bokeh:


import bokeh
print(bokeh.__version__)

Basic Concepts

Bokeh works by creating plots that can be displayed in web browsers. The library follows a hierarchical structure with Figure, ColumnDataSource, and various tools.

Creating Your First Plot

Let's create a simple line plot to understand the basics:


from bokeh.plotting import figure, show
import numpy as np

# Create some sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create a new plot
p = figure(title="Simple Sine Wave", x_axis_label='x', y_axis_label='sin(x)')

# Add a line
p.line(x, y, line_width=2)

# Show the plot
show(p)

Working with ColumnDataSource

The ColumnDataSource is a fundamental data structure in Bokeh that makes it easy to handle data for plots:


from bokeh.models import ColumnDataSource
from bokeh.plotting import figure, show

# Create data
data = {
    'x': [1, 2, 3, 4, 5],
    'y': [2, 5, 8, 2, 7],
    'colors': ['red', 'blue', 'green', 'yellow', 'orange']
}

# Create ColumnDataSource
source = ColumnDataSource(data)

# Create plot
p = figure(title="Scatter Plot with ColumnDataSource")
p.circle('x', 'y', color='colors', size=20, source=source)

show(p)

Adding Interactivity

Bokeh's strength lies in its interactive features. Here's how to add hover tools:


from bokeh.models import HoverTool

# Create plot with hover tool
p = figure(title="Interactive Plot")
p.add_tools(HoverTool(tooltips=[
    ('x', '@x'),
    ('y', '@y')
]))

# Add data points
p.circle('x', 'y', source=source, size=20)

show(p)

Layouts and Arrangements

Bokeh provides various ways to arrange multiple plots. Here's an example using gridplot:


from bokeh.layouts import gridplot

# Create multiple plots
p1 = figure(title="Plot 1")
p1.line([1,2,3], [4,5,6])

p2 = figure(title="Plot 2")
p2.circle([1,2,3], [4,5,6])

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

Saving and Sharing

You can save Bokeh plots to HTML files for sharing:


from bokeh.io import save, output_file

# Set output file
output_file("my_plot.html")

# Create and save plot
p = figure(title="Saved Plot")
p.line([1,2,3], [4,5,6])
save(p)

Customization Options

Bokeh offers extensive customization options for your plots:


p = figure(
    title="Customized Plot",
    background_fill_color="lightgray",
    border_fill_color="white",
    plot_height=300,
    plot_width=500,
    tools="pan,box_zoom,reset"
)

# Customize axes
p.xaxis.axis_label = "X Axis"
p.yaxis.axis_label = "Y Axis"
p.xaxis.axis_label_text_font_size = "14pt"

show(p)

Best Practices

Performance optimization is crucial when working with large datasets. Use ColumnDataSource for efficient data handling and avoid unnecessary plot updates.

Always provide clear titles and labels to make your visualizations more understandable. Use appropriate tools and interactions based on your data presentation needs.

Conclusion

Bokeh is a versatile library for creating interactive visualizations in Python. With its rich feature set and customization options, you can create compelling data presentations for web browsers.

Remember to consult the official Bokeh documentation for more advanced features and updates. Regular practice with different plot types and interactions will help you master this powerful library.