Interactive visualization of data using Bokeh
Last Updated :
28 Mar, 2022
Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If you’ve worked with visualization in Python before, it’s likely that you have used matplotlib. But Bokeh differs from matplotlib.
To install Bokeh type the below command in the terminal.
pip install bokeh
Why you should use Bokeh?
The intended uses of matplotlib and Bokeh are quite different. Matplotlib creates static graphics that are useful for quick and simple visualizations, or for creating publication-quality images. Bokeh creates visualizations for display on the web (whether locally or embedded in a webpage) and most importantly, the visualizations are meant to be highly interactive. Matplotlib does not offer either of these features.
If would you like to visually interact with your data or you would like to distribute interactive visual data to a web audience, Bokeh is the library for you! If your main interest is producing finalized visualizations for publication, matplotlib may be better, although Bokeh does offer a way to create static graphics.
Plotting a simple graph
For this example we will be using one of the built-in dataset, i.e flowers data set. we can use circle() method to plot each data points as a circle on graph, we can also specify custom attributes like:
- first two elements has to be data on x-axis and y-axis respectively.
- color: to assign color dynamically as shown.
- fill_alpha: to assign opacity for circles.
- size: to assign size of each circle.
Example:
Python
from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.iris import flowers
# assign custom colors to represent each
# class of data in a dictionary format
colormap = {'setosa': 'red', 'versicolor': 'green',
'virginica': 'blue'}
colors = [colormap[x] for x in flowers['species']]
# title for the graph
p = figure(title="Iris Morphology")
# label on x-axis
p.xaxis.axis_label = 'Petal Length'
# label on y-axis
p.yaxis.axis_label = 'Petal Width'
# plot each datapoint as a circle
# with custom attributes.
p.circle(flowers["petal_length"],
flowers["petal_width"],
color=colors,
fill_alpha=0.3,
size=15)
# you can save the output as an
# interactive html file
output_file("iris1.html", title="iris.py example")
# display the generated plot of graph
show(p)
Output:

In the above example, output_file() function is used to save the output generated as an html file as bokeh uses web format to provide interactive display. Finally show() function is used to display the generated output.
Note:
- Red color = Setosa, Green = Versicolor, Blue = Virginica
- On top right of every visualization, there are interactive functions provided by bokeh. it allows 1. Pan across plot, 2. Zoom using box selection, 3. Zoom using scroll wheel, 4. Save, 5. Reset, 6. Help
Plotting a bar chart
For this example we will be using custom created data set using list in code itself, i.e fruits data set. output_file() function is used to save the output generated as an html file as bokeh uses web format. we can use ColumnDataSource() function to map the custom data set (two lists) created with each other as a dictionary format. figure() function is used to initialize the graph figure so that data can be plotted on it with various parameters such as:
- x_range: defines data on x-axis.
- plot_width, plot_height: defines width and height of graph.
- toolbar_location: defines location of toolbar.
- title: defines title of graph.
Here we are using simple vertical bars to represent data hence we use vbar() method and to assign various attributes to vertical bars we pass in different parameters inside it, like:
- x: data in x-axis direction
- top: data in y-axis direction
- width: defines width of each bar
- source: source of data
- legend_field: display list of classes present in data
- line_color: defines color for lines in graph
- fill_color: define different colors for data classes
There are many more parameters that can be passed here. Some other properties that can be used are:
- y_range.start: used to define lower limit of data on y-axis.
- y_range.end: used to define upper most limit of data on y-axis.
- legend.orientation: defines orientation of legend bar.
- legend.location: defines location of legend bar.
Finally show() function is used to display the generated output.
Example:
Python
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral10
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
output_file("fruits_bar_chart.html") #output save file name
# creating custom data
fruits = ['Apples', 'Pears', 'Nectarines',
'Plums', 'Grapes', 'Strawberries',
'bananas','berries','pineapples','litchi']
counts = [51, 34, 4, 28, 119, 79, 15, 68, 26, 88]
# mapping counts with classes as a dictionary
source = ColumnDataSource(data=dict(fruits=fruits,
counts=counts))
# initializing the figure
p = figure(x_range=fruits,
plot_width=800,
plot_height=350,
toolbar_location=None,
title="Fruit Counts")
# assigning various attributes to plot
p.vbar(x='fruits', top='counts',
width=1, source=source,
legend_field="fruits",
line_color='white',
fill_color=factor_cmap('fruits',
palette=Spectral10,
factors=fruits))
p.xgrid.grid_line_color = None
p.y_range.start = 0
p.y_range.end = 150
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
# display output
show(p)
Output:

Note: This is a static graph which is also provided by bokeh similar to matplotlib.
Similar Reads
Python - Data visualization tutorial Data visualization is the process of converting complex data into graphical formats such as charts, graphs, and maps. It allows users to understand patterns, trends, and outliers in large datasets quickly and clearly. By transforming data into visual elements, data visualization helps in making data
5 min read
What is Data Visualization and Why is It Important? Data visualization uses charts, graphs and maps to present information clearly and simply. It turns complex data into visuals that are easy to understand.With large amounts of data in every industry, visualization helps spot patterns and trends quickly, leading to faster and smarter decisions.Common
4 min read
Data Visualization using Matplotlib in Python Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. Visualizing Data with P
11 min read
Data Visualization with Seaborn - Python Seaborn is a popular Python library for creating attractive statistical visualizations. Built on Matplotlib and integrated with Pandas, it simplifies complex plots like line charts, heatmaps and violin plots with minimal code.Creating Plots with SeabornSeaborn makes it easy to create clear and infor
9 min read
Data Visualization with Pandas Pandas is a powerful open-source data analysis and manipulation library for Python. The library is particularly well-suited for handling labeled data such as tables with rows and columns. Pandas allows to create various graphs directly from your data using built-in functions. This tutorial covers Pa
6 min read
Plotly for Data Visualization in Python Plotly is an open-source Python library designed to create interactive, visually appealing charts and graphs. It helps users to explore data through features like zooming, additional details and clicking for deeper insights. It handles the interactivity with JavaScript behind the scenes so that we c
12 min read
Data Visualization using Plotnine and ggplot2 in Python Plotnine is a Python data visualization library built on the principles of the Grammar of Graphics, the same philosophy that powers ggplot2 in R. It allows users to create complex plots by layering components such as data, aesthetics and geometric objects.Installing Plotnine in PythonThe plotnine is
6 min read
Introduction to Altair in Python Altair is a declarative statistical visualization library in Python, designed to make it easy to create clear and informative graphics with minimal code. Built on top of Vega-Lite, Altair focuses on simplicity, readability and efficiency, making it a favorite among data scientists and analysts.Why U
4 min read
Python - Data visualization using Bokeh Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps. Bokeh provides two visualization interfaces to us
4 min read
Pygal Introduction Python has become one of the most popular programming languages for data science because of its vast collection of libraries. In data science, data visualization plays a crucial role that helps us to make it easier to identify trends, patterns, and outliers in large data sets. Pygal is best suited f
5 min read