Python - Data visualization using Bokeh
Last Updated :
22 May, 2024
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 users:
bokeh.models : A low level interface that provides high flexibility to application developers.
bokeh.plotting : A high level interface for creating visual glyphs.
To install bokeh package, run the following command in the terminal:
pip install bokeh
The dataset used for generating bokeh graphs is collected from Kaggle.
Code #1: Scatter Markers
To create scatter circle markers, circle() method is used.
Python 1==
# import modules
from bokeh.plotting import figure, output_notebook, show
# output to notebook
output_notebook()
# create figure
p = figure(plot_width = 400, plot_height = 400)
# add a circle renderer with
# size, color and alpha
p.circle([1, 2, 3, 4, 5], [4, 7, 1, 6, 3],
size = 10, color = "navy", alpha = 0.5)
# show the results
show(p)
Output :
Code #2: Single line
To create a single line, line() method is used.
Python 1==
# import modules
from bokeh.plotting import figure, output_notebook, show
# output to notebook
output_notebook()
# create figure
p = figure(plot_width = 400, plot_height = 400)
# add a line renderer
p.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5],
line_width = 2, color = "green")
# show the results
show(p)
Output :
Code #3: Bar Chart
Bar chart presents categorical data with rectangular bars. The length of the bar is proportional to the values that are represented.
Python 1==
# import necessary modules
import pandas as pd
from bokeh.charts import Bar, output_notebook, show
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r"D:/kaggle/mcdonald/menu.csv")
# create bar
p = Bar(df, "Category", values = "Calories",
title = "Total Calories by Category",
legend = "top_right")
# show the results
show(p)
Output :
Code #4: Box Plot
Box plot is used to represent statistical data on a plot. It helps to summarize statistical properties of various data groups present in the data.
Python 1==
# import necessary modules
from bokeh.charts import BoxPlot, output_notebook, show
import pandas as pd
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")
# create bar
p = BoxPlot(df, values = "Protein", label = "Category",
color = "yellow", title = "Protein Summary (grouped by category)",
legend = "top_right")
# show the results
show(p)
Output :
Code #5: Histogram
Histogram is used to represent distribution of numerical data. The height of a rectangle in a histogram is proportional to the frequency of values in a class interval.
Python 1==
# import necessary modules
from bokeh.charts import Histogram, output_notebook, show
import pandas as pd
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")
# create histogram
p = Histogram(df, values = "Total Fat",
title = "Total Fat Distribution",
color = "navy")
# show the results
show(p)
Output :
Code #6: Scatter plot
Scatter plot is used to plot values of two variables in a dataset. It helps to find correlation among the two variables that are selected.
Python 1==
# import necessary modules
from bokeh.charts import Scatter, output_notebook, show
import pandas as pd
# output to notebook
output_notebook()
# read data in dataframe
df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")
# create scatter plot
p = Scatter(df, x = "Carbohydrates", y = "Saturated Fat",
title = "Saturated Fat vs Carbohydrates",
xlabel = "Carbohydrates", ylabel = "Saturated Fat",
color = "orange")
# show the results
show(p)
Output :
Similar Reads
Python - Data visualization tutorial Data visualization is a crucial aspect of data analysis, helping to transform analyzed data into meaningful insights through graphical representations. This comprehensive tutorial will guide you through the fundamentals of data visualization using Python. We'll explore various libraries, including M
7 min read
What is Data Visualization and Why is It Important? Data visualization is the graphical representation of information. In this guide we will study what is Data visualization and its importance with use cases.Understanding Data VisualizationData visualization translates complex data sets into visual formats that are easier for the human brain to under
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. These visualizations he
10 min read
Data Visualization with Seaborn - Python Seaborn is a widely used Python library used for creating statistical data visualizations. It is built on the top of Matplotlib and designed to work with Pandas, it helps in the process of making complex plots with fewer lines of code. It specializes in visualizing data distributions, relationships
9 min read
Data Visualization with Pandas Pandas allows to create various graphs directly from your data using built-in functions. This tutorial covers Pandas capabilities for visualizing data with line plots, area charts, bar plots, and more.Introducing Pandas for Data VisualizationPandas is a powerful open-source data analysis and manipul
5 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 Plotnoine is a Python library that implements a grammar of graphics similar to ggplot2 in R. It allows users to build plots by defining data, aesthetics, and geometric objects. This approach provides a flexible and consistent method for creating a wide range of visualizations. It is built on the con
7 min read
Introduction to Altair in Python Altair is a statistical visualization library in Python. It is a declarative in nature and is based on Vega and Vega-Lite visualization grammars. It is fast becoming the first choice of people looking for a quick and efficient way to visualize datasets. If you have used imperative visualization libr
5 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