Last Updated : 29 Nov, 2022
Bokeh is a Python interactive data visualization. It renders its plots using HTML and JavaScript. It targets modern web browsers for presentation providing elegant, concise construction of novel graphics with high-performance interactivity. Bokeh can be used to visualize the Iris flower dataset. Visualization is done using the plotting module. Here we will be using the Iris dataset given to us by Bokeh.
Downloading the dataset:To download the Iris dataset run the following command on the command line :
bokeh sampledata
Alternatively, we can also execute the following Python code :
import bokeh bokeh.sampledata.download()Analyzing the dataset:
In the sample data provided by Bokeh, there is a file iris.csv, this is the Iris dataset. Below is a glimpse into the iris.csv file :
sepal_length sepal_width petal_length petal_width species 5.1 3.5 1.4 0.2 setosa 4.9 3 1.4 0.2 setosa 4.7 3.2 1.3 0.2 setosa 4.6 3.1 1.5 0.2 setosa 5 3.6 1.4 0.2 setosa
The dataset contains 5 attributes which are :
Each species has 50 records and the total entries are 150.
Visualizing the Dataset:We will be plotting graphs to visualize the clustering of the data for all the 3 species.
Example 1: Here will be plotting a graph with length of petals as the x-axis and the breadth of petals as the y-axis.
Example:
Python3
# importing the modules
from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure, show, output_file
# file to save the model
output_file("gfg.html")
# instantiating the figure object
graph = figure(title="Iris Visualization")
# labeling the x-axis and the y-axis
graph.xaxis.axis_label = "Petal Length (in cm)"
graph.yaxis.axis_label = "Petal Width (in cm)"
# plotting for setosa petals
x = flowers[flowers["species"] == "setosa"]["petal_length"]
y = flowers[flowers["species"] == "setosa"]["petal_width"]
color = "blue"
legend_label = "setosa petals"
graph.circle(x, y,
color=color,
legend_label=legend_label)
# plotting for versicolor petals
x = flowers[flowers["species"] == "versicolor"]["petal_length"]
y = flowers[flowers["species"] == "versicolor"]["petal_width"]
color = "yellow"
legend_label = "versicolor petals"
graph.circle(x, y,
color=color,
legend_label=legend_label)
# plotting for virginica petals
x = flowers[flowers["species"] == "virginica"]["petal_length"]
y = flowers[flowers["species"] == "virginica"]["petal_width"]
color = "red"
legend_label = "virginica petals"
graph.circle(x, y,
color=color,
legend_label=legend_label)
# relocating the legend table to
# avoid abstruction of the graph
graph.legend.location = "top_left"
# displaying the model
show(graph)
Output:
Code Explanation:
Here will be plotting a scatter plot graph with both sepals and petals with length as the x-axis and breadth as the y-axis.
# importing the modules
from bokeh.sampledata.iris import flowers
from bokeh.plotting import figure, show, output_file
# file to save the model
output_file("gfg.html")
# instantiating the figure object
graph = figure(title="Iris Visualization")
# labeling the x-axis and the y-axis
graph.xaxis.axis_label = "Length (in cm)"
graph.yaxis.axis_label = "Width (in cm)"
# plotting for setosa petals
x = flowers[flowers["species"] == "setosa"]["petal_length"]
y = flowers[flowers["species"] == "setosa"]["petal_width"]
marker = "circle_cross"
line_color = "blue"
fill_color = "lightblue"
fill_alpha = 0.4
size = 10
legend_label = "setosa petals"
graph.scatter(x, y,
marker=marker,
line_color=line_color,
fill_color=fill_color,
fill_alpha=fill_alpha,
size=size,
legend_label=legend_label)
# plotting for setosa sepals
x = flowers[flowers["species"] == "setosa"]["sepal_length"]
y = flowers[flowers["species"] == "setosa"]["sepal_width"]
marker = "square_cross"
line_color = "blue"
fill_color = "lightblue"
fill_alpha = 0.4
size = 10
legend_label = "setosa sepals"
graph.scatter(x, y,
marker=marker,
line_color=line_color,
fill_color=fill_color,
fill_alpha=fill_alpha,
size=size,
legend_label=legend_label)
# plotting for versicolor petals
x = flowers[flowers["species"] == "versicolor"]["petal_length"]
y = flowers[flowers["species"] == "versicolor"]["petal_width"]
marker = "circle_cross"
line_color = "yellow"
fill_color = "lightyellow"
fill_alpha = 0.4
size = 10
legend_label = "versicolor petals"
graph.scatter(x, y,
marker=marker,
line_color=line_color,
fill_color=fill_color,
fill_alpha=fill_alpha,
size=size,
legend_label=legend_label)
# plotting for versicolor sepals
x = flowers[flowers["species"] == "versicolor"]["sepal_length"]
y = flowers[flowers["species"] == "versicolor"]["sepal_width"]
marker = "square_cross"
line_color = "yellow"
fill_color = "lightyellow"
fill_alpha = 0.4
size = 10
legend_label = "versicolor sepals"
graph.scatter(x, y,
marker=marker,
line_color=line_color,
fill_color=fill_color,
fill_alpha=fill_alpha,
size=size,
legend_label=legend_label)
# plotting for virginica petals
x = flowers[flowers["species"] == "virginica"]["petal_length"]
y = flowers[flowers["species"] == "virginica"]["petal_width"]
marker = "circle_cross"
line_color = "red"
fill_color = "lightcoral"
fill_alpha = 0.4
size = 10
legend_label = "virginica petals"
graph.scatter(x, y,
marker=marker,
line_color=line_color,
fill_color=fill_color,
fill_alpha=fill_alpha,
size=size,
legend_label=legend_label)
# plotting for virginica sepals
x = flowers[flowers["species"] == "virginica"]["sepal_length"]
y = flowers[flowers["species"] == "virginica"]["sepal_width"]
marker = "square_cross"
line_color = "red"
fill_color = "lightcoral"
fill_alpha = 0.4
size = 10
legend_label = "virginica sepals"
graph.scatter(x, y,
marker=marker,
line_color=line_color,
fill_color=fill_color,
fill_alpha=fill_alpha,
size=size,
legend_label=legend_label)
# relocating the legend table to
# avoid abstruction of the graph
graph.legend.location = "top_left"
# displaying the model
show(graph)
Output:
Code Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4