Data Visualization in Python using Bokeh Library
Bokeh is a Python library which is used for data visualization through high-
performance interactive charts and plots. It creates its plots using HTML and
JavaScript languages. The output of the bokeh library can be generated on
several platforms such as browser, HTML, server, and notebook. It is also
possible to create the bokeh plots in Django and flask applications.
The bokeh library provides two visualization interfaces to the users:
models: it is a low-level interface which provides high flexibility to the
application developers.
plotting: it is a high-level interface which is used for creating visual
glyphs.
How to create different types of data visualization graphs and charts using the
bokeh library in Python.
Installation:
To install the bokeh library, we can use the following command:
!pip3 install bokeh
We can create Scatter Circle Markers on the Plot by using the bokeh library. For
this, we will use the circle() function.
Example:
# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_notebook as ON
from bokeh.plotting import show
# output to the notebook
ON()
# creating figure
plot1 = fig(plot_width = 500, plot_height = 500, title = "Scatter Markers")
# adding a circle renderer with size, color and alpha
plot1.circle([1, 2, 3, 4, 5, 6], [2, 1, 6, 8, 0],
size = 12, color = "green", alpha = 1)
# show the results
show(plot1)
Output:
To Create a Single Line
We can create a single line by using the bokeh library in Python. For this, we
will use the line() function.
Example:
# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_notebook as ON
from bokeh.plotting import show
# output to the notebook
ON()
# creating figure
plot1 = fig(plot_width = 500, plot_height = 500, title = "Scatter Markers")
# adding a line renderer
plot1.line([1, 2, 3, 4, 5, 6], [2, 1, 6, 8, 0],
line_width = 4, color = "red")
# show the results
show(plot1)
Output:
To Create a Bar chart
Bar charts are used for representing the categorical data with rectangular bars.
The length of the bar is proportional to the values they are representing.
Example: Vertical Bar Chart
# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_file as OF
from bokeh.plotting import show
# file to save the model
output_file("jtp.html")
# Now, instantiate the figure object
graph1 = fig(title = "Bokeh Bar Graph")
# x-coordinates to be plotted
x = [1, 2, 3, 4, 5, 6, 7, 8]
# y-coordinates of the top edges
top1 = [5, 1, 4, 3, 2, 7, 6, 8]
# width / thickness of the bars
width1 = 0.7
# plot the graph
graph1.vbar(x,
top = top1,
width = width1,
color = "green")
# display the model
show(graph1)
Output:
Horizontal Bar Chart
# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_file as OF
from bokeh.plotting import show
# file to save the model
output_file("jtp.html")
# Now, instantiate the figure object
graph1 = fig(title = "Bokeh Bar Graph")
# x-coordinates to be plotted
x = [1, 2, 3, 4, 5, 6, 7, 8]
# y-coordinates of the top edges
right1 = [5, 1, 4, 3, 2, 7, 6, 8]
# width / thickness of the bars
height1 = 0.7
# plot the graph
graph1.hbar(x,
right = right1,
height = height1,
color = "green")
# display the model
show(graph1)
Output:
To Create Box Plot
Box plot is used for representing the statistical data on the plots, and it is helpful
for summarizing the statistical properties of several groups present in the
dataset.
To Create Scatter Plot
A Scatter plot is used for plotting values of two variables in a dataset. It is
helpful in finding the correlation between the two variables which are selected.
# First, we will import the required modules
from bokeh.plotting import figure as fig
from bokeh.plotting import output_file as OF
from bokeh.plotting import show
# file for saving the model
OF("jtp.html")
# instantiate the figure object
graph1 = fig(title = "Bokeh Scatter Graph")
# the points to be plotted on Scatter Plot
x1 = [1.4, 5.1, 5.9, 2.3, 5.6, 8.6, 4.5, 2.1, 3.1, 4.3, 5.5, 4.4, 6.9, 2.1, 4, 5.2, 6.3,
7.2, 7.9, 2]
y1 = [3.4, 2.1, 5.7, 8.5, 4.3, 4.2, 5.7, 6.5, 8.9, 9.1, 1.5, 2.1, 6.8, 1, 6, 5.2, 4.5, 7.4,
7.5, 6.3]
# plott the graph
graph1.scatter(x1, y1)
# display the model
show(graph1)
Output: