Interactive Data Visualization with Python: Bokeh

Techpro Education
11 min readAug 26, 2023

--

It is essential to be explained and identified data, which is considered the oil of the future. Examining the data with graphics to reveal what a data set consisting of thousands of rows and columns tells us makes it very easy to understand this situation, which is confusing. This is where the concept of data visualization comes into play.

Data visualization is an significant concept that allows data scientists, analysts, and programmers to have information about the data in a very short time, as well as helping to present the presentations to be made after analyzing the data more understandably and impressively.

In this paper, we will examine Bokeh Library, one of these visualization libraries, especially for those who use Python programming language.

Bokeh

Bokeh is a Python-based data visualization library. It has a powerful visualization opportunity for those who use Python programming language, also it also creates interactive, engaging, and impressive visuals.

Advantages of Bokeh Library

Here are the main strengths of Bokeh library that distinguish it from other data visualization libraries;

Interactive visualization

Bokeh stands out with its rich interactive visualization features. Users can perform interactions such as zooming, panning, selecting, and searching on the graphics. This makes exploratory data analysis more interactive.

Web support

Another design advantage of Bokeh is that it works harmoniously with web-based applications and visualizations. Bokeh is compatible with HTML, CSS, and JavaScript. This provides users a significant advantage in integrating data visualizations into web-based projects.

Multi-platform support

Bokeh can be used on platforms such as Jupyter Notebook, JupyterLab, Flask, and Django, and applications developed with Python. This flexibility allows Bokeh to be used in different environments and projects, making it a user-friendly library.

Drawing flexibility

Bokeh offers options for using a wide range of features and options drawings. Shortly, Bokeh is a library designed to create more personalized visuals. The display style, color palettes, labels, and axes of the graphics can be customized, and advanced drawing features such as layered graphics, shadows, and background images can be used.

Suitable for large data sets

One of the most prominent features of Bokeh is that it can work very quickly with large data sets. Bokeh can process and visualize extensive data sets more effectively. Bokeh’s performance does not degrade as the data set size increases and dashes. This is a massive advantage in scalable data analysis projects.

These advantages are some of the most important arguments that distinguish Bokeh from other data visualization libraries. In short, Bokeh allows its users powerful and interactive visualization.

Installation of Bokeh Library

Firstly, we need to install the library to use Bokeh. You can easily install Bokeh using the pip package manager. You can install Bokeh using the following command:

pip install bokeh

Line Plot drawing

Now, let’s create a simple line chart using Bokeh library. First, we need to import Bokeh modules and create a data source for Bokeh.

from bokeh.plotting import figure, show
from bokeh.io import output_notebook

# Sets output for Jupyter Notebook
output_notebook()

# Creating data
x = [2, 4, 6, 8, 10]
y = [12, 14, 4, 16, 18]

# Creates a figure for bokeh
p = figure(title='A Simple Line Graph', x_axis_label='X', y_axis_label='Y')

# Creates a line graph
p.line(x, y, legend_label='Line', line_width=2)

# Shows the graph
show(p)

Scatter Plot Drawing

After line plot drawing, we create a simple scatter plot using Bokeh library. In this scatter plot, some parameters are used to customize the plot.

# Creates a new drawing using figure dimensions
p = figure(width=400, height=400)

# Privatizations outside the graph
p.outline_line_width = 7
p.outline_line_alpha = 0.3
p.outline_line_color = "red"

# Creating data
x = [2, 4, 6, 8, 10]
y = [12, 8, 4, 16, 18]

# Creates a Scatter graph
r = p.circle(x, y, size=15, line_color="navy", fill_color="orange", fill_alpha=0.5)

# Shows the graph
show(p)

You can also make a scatter plot using squares instead of circles.

# Creates a new drawing using figure dimensions
p = figure(width=500, height=500)

# Customizations on the outside of the chart
p.outline_line_width = 7
p.outline_line_alpha = 0.3
p.outline_line_color = "purple"

# Creating data
x = [2, 4, 6, 8, 10]
y = [12, 8, 4, 16, 18]
# Create Scatter Plot graph optionally enter size, color, alpha
p.square(x, y, size=[10, 15, 20, 25, 30], line_color="black", color="green", alpha=0.9)

# Shows the graph
show(p)

Styling and Editing on a Chart

Bokeh offers many options to customize your graphic. For example, you can adjust accessory labels, titles, line thickness, and colors.

# Creating data
x = [2, 4, 6, 8, 10]
y = [12, 14, 4, 16, 18]

# Creates a figure for bokeh
p = figure(title='A Simple Line Graph', x_axis_label='X', y_axis_label='Y')

# Creates a line graph
p.line(x, y, legend_label='Line', line_width=6, line_color = 'red')

p.title.text = 'New Title'
p.xaxis.axis_label = 'New X Axis Label'
p.yaxis.axis_label = 'New Y Axis Label'

# Shows the graph
show(p)

Hexbin Graph

Hexbin plots are preferred to visualize the density of points, especially in large datasets. The number of hexagonal cells in the graph indicates how the data points are distributed, with more hexagonal cells representing a denser distribution and fewer hexagonal cells expressing a sparser distribution. Darker colors represent regions with higher density, while lighter colors represent regions with lower density.

from bokeh.palettes import Viridis256
from bokeh.util.hex import hexbin

# Creating data
# Generating n (50000) random x and y points with normal distribution
n = 50000
x = np.random.standard_normal(n)
y = np.random.standard_normal(n)

# Hexbin creation and hexagonal cell edge length definition
bins = hexbin(x, y, 0.1)

# With bins.counts we get the number of dots in each hexagonal cell.
# We normalize these numbers (divide by the maximum number of dots) to the range 0-255.
# Then we select the colors corresponding to these values from the Viridis256 color palette.
color = [Viridis256[int(i)] for i in bins.counts/max(bins.counts)*255]

# Creates a figure for bokeh
p = figure(tools="wheel_zoom,reset", match_aspect=True, background_fill_color='#440154')
p.grid.visible = False

# Plotting the Hexbin graph
p.hex_tile(bins.q, bins.r, size=0.1, line_color=None, fill_color=color)

# Shows the graph
show(p)

image & image_rgba Graphics

Graphics created with these functions can be interpreted in a similar way to heat maps.

Images created with the image function create a grayscale image using a single color palette and can be visualized by converting the value of each pixel into color through the color palette.

With the image_rgba function, RGB(A) (Red, Green, Blue, Alpha) is visualized using pixel colors. This function allows the color values of pixels to be specified directly. In this way, more customized graphics can be designed without having to use a color palette.

# Creating data
# Create an x and y array of N points between 0 and 10
N = 500
x = np.linspace(0, 10, N)
y = np.linspace(0, 10, N)

# You are creating a 2D grid using the arrays x and y.
# The matrices xx and yy will contain the coordinates of each point.
xx, yy = np.meshgrid(x, y)

# Using np.sin(xx) and np.cos(yy) you create an image matrix called img.
# This matrix will contain the values of the coordinates xx and yy calculated according to the sine and cosine functions.
img = np.sin(xx)*np.cos(yy)

# You create a drawing area (p) using the Figure function.
# With the x_range and y_range parameters you define the range of the x and y axes.
p = figure(x_range=(0, 10), y_range=(0, 10))

# We add an image to the drawing area using the image function.
# You give the img matrix as image parameter.
# with the x and y parameters you set the origin of the image.
# The dw and dh parameters set the width and height of the image.
# The palette parameter determines the color palette of the image.
p.image(image=[img], x=0, y=0, dw=10, dh=10, palette="Spectral11")

# Shows the graph
show(p)
from __future__ import division
import numpy as np

# You assign the value 20 to the variable N.
# This is a parameter that determines the size of the image.
# The image will be 20x20 pixels.
N = 20

# You create a Numpy array called img.
# This array represents an image of size N x N.
# dtype=np.uint32 parameter
# Specifies that each pixel will hold RGBA values as a 32-bit integer.
img = np.empty((N,N), dtype=np.uint32)

# You create a new array called view.
# This array provides a view to the img array
# Allows access to the RGBA channels of each pixel individually.
# dtype=np.uint8 parameter
# Specifies that each RGBA channel is kept as an 8-bit integer.
view = img.view(dtype=np.uint8).reshape((N, N, 4))

# Using a for loop, you assign an RGBA value to each pixel.
# These values determine the color of the pixel.
# The red channel (R) changes depending on the horizontal position of the pixel
# Green channel (G) is fixed at 158
# The blue channel (B) varies depending on the vertical position of the pixel.
# The Alpha channel (A) is completely opaque and has a value of 255.
for i in range(N):
for j in range(N):
view[i, j, 0] = int(i/N*255) # red
view[i, j, 1] = 158 # green
view[i, j, 2] = int(j/N*255) # blue
view[i, j, 3] = 255 # alpha

# we create a drawing area (figure).
# The parameters x_range and y_range determine the range of the x and y axes of the drawing area.
p = figure(x_range=[0,10], y_range=[0,10])

# We add an RGBA image to the drawing area using the image_rgba function.
p.image_rgba(image=[img], x=[0], y=[0], dw=[10], dh=[10])

# Shows the graph
show(p)

Interactive Graphics

Bokeh library allows customization of graphics and interactive viewing of graphics. It also allows for more advanced customization, such as customizing axes and adding images to the background.

Glyph

Glyphs are used with drawing tools and plot objects in Bokeh library. One or more Glyphs added to the plot area allow visualization of the dataset and enable users to explore the data interactively.

# Creates a new drawing using the figure dimensions
p = figure(width=400, height=400)

# Privatisations other than the graphic
p.outline_line_width = 7
p.outline_line_alpha = 0.3
p.outline_line_color = "red"

# Creating data
x = [2, 4, 6, 8, 10]
y = [12, 8, 4, 16, 18]

# Creates the Glyph chart
r = p.circle(x, y)

# The size of the apartment is adjusted
r.glyph.size = 50

# The transparency of the interior colour of the apartment is adjusted
r.glyph.fill_alpha = 0.2

# The colour of the edge line of the circle is set
r.glyph.line_color = "firebrick"

# The shape of the edge line of the circle (line_dash) and
# Adjust the length of lines and spaces [5, 1]
r.glyph.line_dash = [5, 1]

# Adjust the thickness of the edge line of the circle
r.glyph.line_width = 2

# Shows the graph
show(p)

Bokeh Data sets

Now, we can make a few examples with the data sets embedded in Bokeh library. First of all, the data sets in Bokeh library need to be downloaded.

import bokeh.sampledata
bokeh.sampledata.download()

Let’s make an interactive chart example with the “glucose” data set in Bokeh library.

# A tool used to display an interactive legend when hovering over data points.
from bokeh.models.tools import HoverTool
from bokeh.sampledata.glucose import data

# From the "glucose" dataset we select the data from 2010-10-06
subset = data.loc['2010-10-06']

# We assign the timestamp (index) values of the selected data to variable x and
# the values in the 'glucose' column to variable y
x, y = subset.index.to_series(), subset['glucose']

# We create a 600 x 300 pixel drawing area (figure).
# The parameter x_axis_type="datetime" specifies that the x-axis is interpreted as a timestamp.
# We also add a title to the drawing area.
p = figure(width=600, height=300, x_axis_type="datetime", title='Hover over points')

# We add a line graph to the drawing area using the line function.
# The x and y variables specify the x and y coordinates of the line.
# We set the line with a dashed pattern with the line_dash parameter,
# the line thickness with the line_width parameter,
# and the line color with the color parameter.
p.line(x, y, line_dash="4 4", line_width=1, color='gray')

# We add circle glyphs to the drawing area using the function
# The x and y variables specify the center coordinates of the circles.
# the size of the circles with the size parameter,
# fill color with fill_color parameter,
# hover_fill_color parameter to set the hover fill color,
# fill transparency with the fill_alpha parameter,
# hover transparency on mouseover with the hover_alpha parameter,
# set the color of the border line with the line_color parameter
# with the hover_line_color parameter we set the color of the border on mouseover.
# With the circle function, we return an object of type GlyphRenderer and assign it to the variable cr.
cr = p.circle(x, y, size=20,
fill_color="grey", hover_fill_color="firebrick",
fill_alpha=0.05, hover_alpha=0.3,
line_color=None, hover_line_color="white")

# with tooltips=None parameter you specify that annotations are not displayed.
# renderers parameter to specify which images (GlyphRenderers)
# we specify to connect to this tool
# with the parameter mode='hline' we specify that the annotations are displayed in a horizontal line mode.
p.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))

# Shows the graph
show(p)
from bokeh.sampledata.autompg import autompg
from bokeh.models import LinearColorMapper, ColorBar, HoverTool
from bokeh.transform import transform
from bokeh.plotting import figure, show
from bokeh.models.sources import ColumnDataSource

source = ColumnDataSource(autompg)
color_mapper = LinearColorMapper(palette="Viridis256", low=autompg.weight.min(), high=autompg.weight.max())

p = figure(x_axis_label='Horsepower', y_axis_label='MPG', tools='hover', toolbar_location=None)
p.circle(x='hp', y='mpg', color=transform('weight', color_mapper), size=20, alpha=0.6, source=autompg)

color_bar = ColorBar(color_mapper=color_mapper, label_standoff=12, location=(0,0), title='Weight')
p.add_layout(color_bar, 'right')

hover = p.select_one(HoverTool)
hover.tooltips = [('MPG', '@mpg'), ('Horsepower', '@hp'), ('Weight', '@weight')]

show(p)

Let’s make another interactive visualization with the Iris data set.

from bokeh.plotting import figure, show
from bokeh.sampledata.iris import flowers
from bokeh.transform import factor_cmap
from bokeh.palettes import Category10

# Load data set
df = flowers.copy()

# Create a color palette according to flower types
color_map = factor_cmap('species', palette=Category10[3], factors=df['species'].unique())

# Enable Jupyter Notebook output
output_notebook()

# Create Figure
p = figure(title='Iris Çiçekleri', x_axis_label='Sepal Length', y_axis_label='Petal Length')

# Draw scatter plot
scatter = p.circle(x='sepal_length', y='petal_length', size=8, color=color_map, legend_field='species', source=df)

# Create HoverTool
hover = HoverTool(tooltips=[('Sepal Length', '@sepal_length'), ('Petal Length', '@petal_length')], renderers=[scatter])

# Add HoverTool to Figure
p.add_tools(hover)

# Positioning Legend
p.legend.location = 'top_left'

# Show Plot
show(p)

Bokeh is a Python-based visualization library for transforming data into interactive and professional-looking graphs. Bokeh has a wide variety of charts, giving users the opportunity to create line graphs, scatter plots, bar charts, area charts, histograms, maps, and more.

One of the most essential features of Bokeh is that it gives users the opportunity for interactive graphics, as seen above. Users can add mouse interaction to the graphs and use features such as tooltips, panning, and zooming. Thus, users can examine the data more thoroughly and navigate the graphs.

Color palettes and color mapping methods are another powerful feature of Bokeh. In this way, data can be colored to highlight categories or value ranges, making the visualization easier to understand.

In conclusion, Bokeh is a powerful library for transforming data into attractive and interactive graphics. With Bokeh’s wide range of charting options, interactive features, and coloring capabilities, you can make data analysis and presentation more impressive.

We hope you find it useful.

--

--

Techpro Education
Techpro Education

Written by Techpro Education

TechPro Education is a coding bootcamp founded in 2019 by a group of passionate IT field experts with 10 years of teaching experience. www.techproeducation.com