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

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

Himanshu Sharma
Bokeh Tutorial
W3Schools

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, 

See Also

         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.

What Do You Think?

If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top