Image for post
Image for post

Visualizing Data with Python’s Bokeh Package and Jupyter Notebook

This tutorial is based on the YouTube Video Python Data Visualization with Bokeh. The goal here is to provide documentation for myself as I’m starting to learn Bokeh. I’m hoping it’ll be useful for you as well.

One main difference between this tutorial and the video is that I’m going to use a jupyter notebook to execute my python.

Let’s dive in.

p.s. just an idea: if you get some value out of this article, and you’d like to support the author, purchase your next Amazon shipment through my affiliate link here. Cheers!

Step 1: Getting Started with a Line Graph

To get started I’m going to create a new directory to store these files in, spin up my python 3 environment, and then jump into a jupyter notebook.

I’m making the assumption that you’ve already installed Anaconda Navigator, have set up an environment to run Python3, and have installed Jupyter notebook.

If not, install Anaconda Navigator and perform these steps. Need help? Leave a comment here and I’ll write up some documentation.

Onwards.

In the terminal — I use iTerm — I write:

#this creates the folder
mkdir bokeh
#change directory into the folder
cd bokeh
#activate the environment I've set up using Anaconda navigator
conda activate python3
#launch jupyter notebook
jupyter notebook

Next, in the first cell of our jupyter notebook, we need to import the necessary packages:

import pandas as pd
from bokeh.plotting import figure, output_file, show

Next, we’ll tell the notebook where to save the output:

output_file('index.html')

Now we’ll create some fake data just to fire a tracer and make sure everything is working correctly:

x = [1,2,3,4,5]
y = [4,6,2,4,3]

Alright. Now we’ll define the title, x-axis label, y-axis label, width, and height of the figure container the will house our series of data:

p = figure(
title = 'Simple Example',
x_axis_label ='X Axis',
y_axis_label ='Y Axis',
plot_width=600,
plot_height=400
)

Moving on, we’ll plot a line inside the figure named “p”:

p.line(x,y, legend='Test', line_width=2)

Then we need to show the plot:

show(p)

You’ll end up with something that looks like:

Image for post
Image for post

Your jupyter notebook should look something like this:

Image for post
Image for post

Step 2: Importing Data from a CSV

In his tutorial Python Data Visualization with Bokeh, Brad uses a CSV of cars available on his github repo here.

We’ll use the pandas package to read the CSV directly from the raw github URL into our jupyter notebook.

url = 'https://raw.githubusercontent.com/bradtraversy/python_bokeh_chart/master/cars.csv'

Then use the pandas read_csv function to save the data into a dataframe called “csv”:

df = pd.read_csv(url)

Open up a new cell in your notebook and run “df”. You’ll see a well-formatted dataframe that looks like:

Image for post
Image for post

Now we can easily access any data in this dataframe. Try the following commands in your notebook to get a feel for how you can access data from a dataframe:

car = df['Car']
horsepower = df['Horsepower']
price = df['Price']
image = df['Image']

Then run each variable in a new cell and see what you get. “car” will render:

Image for post
Image for post

Step 3: Plotting Data with a Horizontal Bar Chart

Now that we have our data, let’s plot the horsepower by car on a horizontal bar chart.

Set up a new figure, adding a few more parameters that we did with the line chart. (Notice we’re setting tools to “”.)

p = figure(
y_range=car,
title = 'Cars With Top Horsepower',
x_axis_label ='Horsepower',
plot_width=800,
plot_height=600,
tools=""
)

Now for the data. We’ll plot the cars on the y-axis.

p.hbar(
y=car,
right=horsepower,
left=0,
height=0.4,
color='orange',
fill_alpha=0.5
)

Next, where do you want to save this visualization? I’m going to use ‘cars.html’ as my output file:

output_file('cars.html')

And then render the chart!

show(p)

You’ll get something like this:

Image for post
Image for post

And your notebook will look something like this:

Image for post
Image for post

You’ve already imported pandas and bokeh in the cells at the top of your notebook. No need to import them again.

Step 4: Adding Bokeh Tools

You noticed that we set tools to “” when we created our figure above. Now, we’ll learn how to add the specific tools we want to our plot:

  • pan
  • box_select
  • zoom_in
  • zoom_out
  • save
  • reset

Documentation on Bokeh’s tools available here.

Add these tools, and your figure cell will look like:

p = figure(
y_range=car,
title = 'Cars With Top Horsepower',
x_axis_label ='Horsepower',
plot_width=800,
plot_height=600,
tools="pan,box_select,zoom_in,zoom_out,save,reset"
)

Run all your cells again, and you’ll see the toolbar on the right here:

Image for post
Image for post

Step 5: Import ColumnDataSource

First, why ColumnDataSource? ColumnDataSource allows you to map visual properties to Data Columns. Video clip on the subject here.

In our top cell, where we’ve imported our packages, import ColumnDataSource:

import pandas as pd
from bokeh.plotting import figure,output_file,show,ColumnDataSource

Then create a new variable called “source” and set equal to ColumnDataSource, passing your dataframe in as an argument:

source = ColumnDataSource(df)

Then we’ll create a list of the cars that we are plotting:

car_list = source.data['Car'].tolist()

Next, pass car_list into our figure as the y_range:

p = figure(
y_range=car_list,
title = 'Cars With Top Horsepower',
x_axis_label ='Horsepower',
plot_width=800,
plot_height=600,
tools="pan,box_select,zoom_in,zoom_out,save,reset"
)

Then we’ll reconfigure our plot, setting parameter “source” to the variable we just created called “source”. We will also update the y value to ‘Car’ and the right value to ‘Horsepower’.

p.hbar(
y='Car',
right='Horsepower',
left=0,
height=0.4,
color='orange',
fill_alpha=0.5,
source=source
)

My notebook looks like:

Image for post
Image for post

Go ahead and run the cells and show our plot. You’ll see that nothing has really changed yet from a visual point of view:

Image for post
Image for post

Next, you’ll see the power of ColumnDataSource when combined with Tooltips.

Step 6: Import HoverTool

Import the package:

from bokeh.models.tools import HoverTool

Create a variable called “hover” and set equal to HoverTool()

hover = HoverTool()

Then write the HTML that will be rendered when you hover over the plot element:

hover.tooltips = """
<div>
<h3>@Car</h3>
<div><strong>Price: </strong>@Price</div>
<div><strong>HP: </strong>@Horsepower</div>
<div><image src="@Image" alt="" width="200"/></div>
</div>
"""

Next, add the hover tool to your plot with ColumnDataSource:

p.add_tools(hover)

Go ahead and run the cells, and then finally, run the show(p) command:

show(p)

Now when we hover over the chart elements, we’ll be rewarded with a small rectangular div with the car name, price, horsepower, and image:

Image for post
Image for post

Cool!

The notebook looks like:

Image for post
Image for post

Step 7: Import factor_cmap

Import the packages:

from bokeh.transform import factor_cmap
from bokeh.palettes import Blues8

Update the hbar plot. No need to update the figure.

p.hbar(
y='Car',
right='Horsepower',
left=0,
height=0.4,
fill_color=factor_cmap(
'Car',
palette=Blues8,
factors=car_list
),
fill_alpha=0.9,
source=source
)

Runs those two cells and then show(p)

show(p)

Because our figure and data are all saved in notebook memory, we don’t actually need to run those cells again.

Show the plot show(p) and you should see:

Image for post
Image for post

Step 8: Add Legend

Update our hbar plot, adding legend='Car'

p.hbar(
y='Car',
right='Horsepower',
left=0,
height=0.4,
fill_color=factor_cmap(
'Car',
palette=Blues8,
factors=car_list
),
fill_alpha=0.9,
source=source,
legend='Car'
)

Then,

p.legend.orientation='vertical'
p.legend.location='top_right'
p.legend.label_text_font_size='10px'

Run the cells, and then show(p) . You’ll see:

Image for post
Image for post

Annnnnd that’s all folks. Good night!

Thanks for reading! I hope this article was helpful.

Your comments and claps keep me motivated to create more material. So if you found this useful, leave a comment or just click to “clap.”

I appreciate you!

Written by

Documentation and tutorials on Python, Programming, and Data Analysis. FPL Addict. Occasionally writing about biohacking, PMing, and food.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store