MITB Banner

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

Share

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:

  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.

Share
Picture of Himanshu Sharma

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.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.