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
data
=
load_iris(as_frame
=
True
)
data
=
data.frame
data.target.replace({
0
: load_iris().target_names[
0
],
1
:load_iris().target_names[
1
],
2
:load_iris().target_names[
2
]},
inplace
=
True
)
df
=
data.groupby(
'target'
).agg(
'mean'
)
x
=
[ (
cls
, col)
for
cls
in
data.target.unique()
for
col
in
data.columns[:
-
1
] ]
mean
=
sum
(
zip
(df[
'sepal length (cm)'
],
df[
'sepal width (cm)'
],
df[
'petal length (cm)'
],
df[
'petal width (cm)'
]), ())
source
=
ColumnDataSource(data
=
dict
(x
=
x, Average
=
mean))
output_file(
"Bokeh Nested Bar chart.html"
)
p
=
figure(x_range
=
FactorRange(
*
x),
height
=
350
,
title
=
"Iris Flower Average length"
,
)
color
=
[
'orange'
,
'#FF0000'
,
'green'
,
'#00FF00'
,]
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
)
)
p.xaxis.major_label_orientation
=
1
p.xgrid.grid_line_color
=
None
show(p)