Bokeh Interactive Visualizations Data Visualization Jupyter Notebook Python Line Chart Scatter Plot ColumnDataSource Figure Glyphs

Creating Interactive Visualizations with Bokeh in Jupyter Notebook

2023-05-01 11:30:20

//

5 min read

Blog article placeholder

Creating Interactive Visualizations with Bokeh in Jupyter Notebook

Bokeh is an interactive visualization library in Python that allows you to create beautiful and interactive visualizations with ease. One of the best things about using Bokeh is that it provides a simple and easy-to-use interface for creating interactive and dynamic visualizations.

In this tutorial, we will explore how to create interactive visualizations with Bokeh in Jupyter Notebook.

Getting Started with Bokeh

Before we start creating interactive visualizations with Bokeh, we need to install and import the necessary packages. Run the following commands to install and import Bokeh:

!pip install bokeh
from bokeh.plotting import figure, output_notebook, show

The figure function allows us to create a new figure for plotting. The output_notebook function enables us to plot the visualizations in the Jupyter Notebook. The show function displays the plot in the Notebook.

Creating a Simple Line Chart

Let's start by creating a simple line chart. Create a new Figure object with a title and x- and y-axis labels.

## Import the necessary packages
from bokeh.models import ColumnDataSource
from bokeh.io import output_notebook, show
from bokeh.plotting import figure

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

## Create a new plot with a title and axis labels
p = figure(title='Simple Line Chart in Bokeh', x_axis_label='X', y_axis_label='Y')

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

## Display the plot
show(p)

This code will generate a simple line chart with a title, x- and y-axis labels, and a line renderer. The output will be shown in the Jupyter Notebook.

Creating an Interactive Scatter Plot

Bokeh allows us to create interactive visualizations with its wide range of tools. Let's create an interactive scatter plot using Bokeh. Create a new ColumnDataSource with our data and create a new Figure object with x- and y-axis labels.

## Import the necessary packages
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral6
from bokeh.plotting import figure, output_notebook, show
from bokeh.transform import factor_cmap

## Define data
fruits = ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
counts = [5, 3, 4, 2, 4]

## Create a ColumnDataSource object
source = ColumnDataSource(data=dict(fruits=fruits, counts=counts))

## Create a new plot with a title and axis labels
p = figure(title='Interactive Scatter Plot in Bokeh', x_axis_label='Fruits', y_axis_label='Counts')

## Add circle glyphs to the plot
p.circle('fruits', 'counts', source=source, size=12, line_color='black', fill_color=factor_cmap('fruits', palette=Spectral6, factors=fruits))

## Display the plot
show(p)

This code will generate an interactive scatter plot with circle glyphs and a legend based on fruit types.

Conclusion

Bokeh is an incredibly useful Python library for creating interactive and beautiful visualizations. With its simple and easy-to-use interface, you can create a wide range of visualizations in Jupyter Notebook. In this tutorial, we explored how to create a simple line chart and an interactive scatter plot using Bokeh. Now it's time for you to start exploring and experimenting with Bokeh to create your own data visualizations.