# Import the necessary module
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, FactorRange
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
from sklearn.datasets import load_iris
# Load the iris dataset
data = load_iris(as_frame=True)
data= data.frame
# Replace 0, 1, 2 with respective iris flower name
data.target.replace({0: load_iris().target_names[0],
1:load_iris().target_names[1],
2:load_iris().target_names[2]},
inplace = True)
# CGroup the iris flower by the average length value
df = data.groupby('target').agg('mean')
# This will create a list with iris flower name and their respective sepal & petal length and width name
x = [ (cls, col) for cls in data.target.unique() for col in data.columns[:-1] ]
# This will contain the respective numerical for each x
mean = sum(zip(df['sepal length (cm)'],
df['sepal width (cm)'],
df['petal length (cm)'],
df['petal width (cm)']), ())
# create the dicti
source= ColumnDataSource(data=dict(x=x, Average = mean))
# file to save the model
output_file("Bokeh Nested Bar chart.html")
# FIgure
p = figure(x_range=FactorRange(*x),
height=350,
title="Iris Flower Average length",
)
#color
color = ['orange','#FF0000','green','#00FF00',]
# Vertical bar chaRT
p.vbar(x='x',
top='Average',
width=0.9,
source=source,
line_color="white",
fill_color=factor_cmap('x', palette=color, factors=data.columns[:-1], start=1, end=2)
)
# Orintation of text
p.xaxis.major_label_orientation = 1
p.xgrid.grid_line_color = None
show(p)