Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Python Advanced - Data Visualization with Bokeh

Creating interactive visualizations with Bokeh in Python

Bokeh is a powerful Python library for creating interactive visualizations for modern web browsers. It provides elegant and concise construction of versatile graphics and affords high-performance interactivity over large or streaming datasets. This tutorial explores how to use Bokeh for creating interactive visualizations in Python.

Key Points:

  • Bokeh is a powerful library for creating interactive visualizations in Python.
  • It provides elegant and concise construction of versatile graphics.
  • Bokeh allows for high-performance interactivity over large or streaming datasets.

Installing Bokeh

To use Bokeh, you need to install it using pip:


pip install bokeh
            

Creating a Simple Plot

Here is an example of creating a simple line plot with Bokeh:


from bokeh.plotting import figure, output_file, show

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

# Create a new plot
p = figure(title="Simple Line Plot", x_axis_label='X-Axis', y_axis_label='Y-Axis')

# Add a line renderer
p.line(x, y, legend_label="Line", line_width=2)

# Specify the output file
output_file("line.html")

# Show the plot
show(p)
            

Creating a Scatter Plot

Here is an example of creating a scatter plot with Bokeh:


from bokeh.plotting import figure, output_file, show

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

# Create a new plot
p = figure(title="Simple Scatter Plot", x_axis_label='X-Axis', y_axis_label='Y-Axis')

# Add a scatter renderer
p.circle(x, y, size=10, color="navy", alpha=0.5)

# Specify the output file
output_file("scatter.html")

# Show the plot
show(p)
            

Creating a Bar Chart

Here is an example of creating a bar chart with Bokeh:


from bokeh.plotting import figure, output_file, show
from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource

# Prepare the data
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]

source = ColumnDataSource(data=dict(fruits=fruits, counts=counts))

# Create a new plot
p = figure(x_range=fruits, plot_height=250, title="Fruit Counts",
           toolbar_location=None, tools="")

# Add a bar renderer
p.vbar(x='fruits', top='counts', width=0.9, source=source, legend_label="Fruits",
       line_color='white', fill_color='navy')

# Customize the plot
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.legend.orientation = "horizontal"
p.legend.location = "top_center"

# Specify the output file
output_file("bar.html")

# Show the plot
show(p)
            

Adding Interactivity

Bokeh provides various tools to add interactivity to your plots. Here is an example of adding hover tools to a scatter plot:


from bokeh.plotting import figure, output_file, show
from bokeh.models import HoverTool

# Prepare the data
x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]
names = ['A', 'B', 'C', 'D', 'E']

# Create a new plot
p = figure(title="Scatter Plot with Hover Tool", x_axis_label='X-Axis', y_axis_label='Y-Axis', tools="")

# Add a scatter renderer
p.circle(x, y, size=10, color="navy", alpha=0.5)

# Add hover tool
hover = HoverTool()
hover.tooltips = [("Index", "$index"), ("(X,Y)", "($x, $y)"), ("Name", "@names")]
p.add_tools(hover)

# Specify the output file
output_file("scatter_hover.html")

# Show the plot
show(p)
            

Creating Linked Plots

Bokeh allows you to create linked plots where interactions in one plot affect another plot. Here is an example of linked brushing between two scatter plots:


from bokeh.layouts import gridplot
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource

# Prepare the data
x = [1, 2, 3, 4, 5]
y0 = [6, 7, 2, 4, 5]
y1 = [7, 8, 3, 5, 6]
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))

# Create two scatter plots
p1 = figure(title="Scatter Plot 1", x_axis_label='X-Axis', y_axis_label='Y-Axis', tools="pan,wheel_zoom,box_zoom,reset")
p1.circle('x', 'y0', size=10, color="navy", alpha=0.5, source=source)

p2 = figure(title="Scatter Plot 2", x_axis_label='X-Axis', y_axis_label='Y-Axis', tools="pan,wheel_zoom,box_zoom,reset", x_range=p1.x_range, y_range=p1.y_range)
p2.circle('x', 'y1', size=10, color="firebrick", alpha=0.5, source=source)

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

# Specify the output file
output_file("linked_plots.html")

# Show the plots
show(layout)
            

Deploying Bokeh Apps

Bokeh allows you to create and deploy interactive web applications. Here is a simple example of a Bokeh app:


# app.py

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure

# Prepare the data
x = [x*0.05 for x in range(0, 200)]
y = [x**2 for x in x]
source = ColumnDataSource(data=dict(x=x, y=y))

# Create a plot
plot = figure(title="Parabola", x_axis_label='X', y_axis_label='Y')
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# Create a slider
slider = Slider(start=0.1, end=10, value=1, step=0.1, title="Scale")

# Define a callback function
def update_data(attrname, old, new):
    scale = slider.value
    y = [scale * (x_val**2) for x_val in x]
    source.data = dict(x=x, y=y)

# Attach the callback to the slider
slider.on_change('value', update_data)

# Arrange the plot and the slider in a layout
layout = column(slider, plot)

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

To run the Bokeh app, use the following command:


bokeh serve --show app.py
            

Summary

In this tutorial, you learned about creating interactive visualizations with Bokeh in Python. Bokeh provides a powerful and flexible way to create interactive and versatile visualizations for modern web browsers. Understanding how to create basic plots, add interactivity, link plots, and deploy Bokeh apps can help you create compelling and interactive visualizations for your data science projects.