pandas_groupby_colormapped#

使用 Auto MPG 数据集 的条形图。此示例演示了 Pandas GroupBy 对象的自动处理以及使用 factor_cmap 的颜色映射。

详细信息

样本数据:

bokeh.sampledata.autompg

Bokeh API:

figure.vbar, bokeh.transform.factor_cmap

更多信息:

使用 pandas

关键字:

条形图,分类,颜色映射,分组,pandas

from bokeh.palettes import Spectral5
from bokeh.plotting import figure, show
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap

df.cyl = df.cyl.astype(str)
group = df.groupby('cyl')

cyl_cmap = factor_cmap('cyl', palette=Spectral5, factors=sorted(df.cyl.unique()))

p = figure(height=350, x_range=group, title="MPG by # Cylinders",
           toolbar_location=None, tools="")

p.vbar(x='cyl', top='mpg_mean', width=1, source=group,
       line_color=cyl_cmap, fill_color=cyl_cmap)

p.y_range.start = 0
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "some stuff"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None

show(p)