Hands-On Tutorial on Bokeh – Open Source Python Library For Interactive Visualizations

Bokeh Tutorial

Dashboards are collections of bars, charts, and graphs that help us visualize different attributes of a dataset. A dashboard works as a graphical user interface which helps us identify the key performance indicators relevant to the dataset or the particular business model. Python provides different open-source libraries that can help you create your own dashboard with your dataset. Today we will be talking about Bokeh which is an open-source python library for interactive visualizations for the modern web browsers. 

Bokeh provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets. It can be used for different purposes like creating interactive plots, dashboards, and even data-driven applications.

In this article we will discuss:

THE BELAMY

Sign up for your weekly dose of what's up in emerging technology.
  1. Creating Bokeh Visualization and Analyzing it.
  2. Creating a Sales Dashboard using Bokeh

Implementation of Bokeh:

Like any other library, we need to install Bokeh for exploring it by pip install bokeh

  1. Importing required libraries

We will import pandas for loading the dataset and will import different functions of bokeh as and when required.

import pandas as pd

from bokeh.plotting import figure, output_file, show

  1. Loading the dataset

We will create a sales dashboard for which we need sales data of a company, here I will use a dataset which contains Sales of a company and different attributes on which it depends.

df = pd.read_csv(‘Advertising.csv’)

df

  1. Creating Visualizations

We will start by creating visualizations and later add them to a single dashboard. Starting with the scatter plots to find the relationship between Sales and the factors governing it.

from bokeh.plotting import figure, output_file, show

output_file("p1.html")

p1 = figure(plot_width=400, plot_height=400)

p1.circle(df['TV'], df['Sales'], size=2, color="navy", alpha=0.5)

show(p1)

As you can see here Bokeh not only creates visualization but also provides different functions as shown in the image, the visualization is highly interactive and downloadable.

Similarly, we will create the visualization for the other two attributes against the Sales.

output_file("p2.html")

p2 = figure(plot_width=400, plot_height=400)

p2.circle(df['Radio'], df['Sales'], size=2, color="navy", alpha=0.5)

show(p2)

output_file("p3.html")

p3 = figure(plot_width=400, plot_height=400)

p3.circle(df['Newspaper'], df['Sales'], size=2, color="navy", alpha=0.5)

show(p3)

Bokeh

As you can see here we have created three scatter plots of Sales(Dependent Variable) vs. Newspaper, TV, Radio( Independent Variable)

Next, we will create the distribution plots according to the ‘Sales’ of all the numerical attributes.

fig1 = figure(plot_width=400, plot_height=400)

fig1.vbar(x=df['Sales'], bottom=0, top=df['TV'],

          color='blue', width=0.75, 

          legend='TV')

           show(fig1)

Bokeh

fig2 = figure(plot_width=400, plot_height=400)

fig2.vbar(x=df['Sales'], bottom=0, top=df['Radio'],

         color='red', width=0.75, 

         legend='Radio')

show(fig2)

Bokeh

fig3 = figure(plot_width=400, plot_height=400)

fig3.vbar(x=df['Sales'], bottom=0, top=df['Newspaper'],

         color='green', width=0.75, 

         legend='Newspaper')

show(fig3)

Bokeh

Similarly, we can create different types of plots and graphs according to our requirements. 

Next, we will design the layout and show these images in a single dashboard.

  1. Creating a Dashboard

For getting all these images in a single layout we need to define the layout of the dashboard.

We will use the grid layout and visualize the dashboard in a grid format.

from bokeh.layouts import gridplot

grid = gridplot([[p1, p2, p3], [fig1, fig2, fig3]], plot_width=250, plot_height=250)

show(grid)

Bokeh

As you can see in the image we have created a dashboard in a grid layout, it clearly shows the relationship between Sales and the numerical on which it is dependent.

This is just a basic layout/Dashboard which is easily created and can help you start designing dashboards in Bokeh if you are a beginner.

Conclusion:

In this article, we learned about dashboards, how they help us to visualize the key performance indicators. We learned about Bokeh and how we can create dashboards using it, Bokeh is highly interactive and visually appealing. We can use this knowledge to create our own dashboards and visualize it.

More Great AIM Stories

Himanshu Sharma
An aspiring Data Scientist currently Pursuing MBA in Applied Data Science, with an Interest in the financial markets. I have experience in Data Analytics, Data Visualization, Machine Learning, Creating Dashboards and Writing articles related to Data Science.

Our Upcoming Events

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

Conference, in-person (Bangalore)
Cypher 2023
20-22nd Sep, 2023

3 Ways to Join our Community

Whatsapp group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our newsletter

Get the latest updates from AIM