how use bokeh vbar chart parameter with groupby ob

2020-05-03 01:19发布

问题:

Question

Below code is grouped vbar chart example from bokeh documentation. There are something i can't understand on this example.

  1. Where 'cyl_mfr' is come from in factor_cmap() and vbar()?

  2. 'mpg_mean' , is it calculating the mean of 'mpg' column? if then, why 'mpg_sum' doesn't work?

I want to make my own vbar chart like this example.


Code

from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.plotting import figure
from bokeh.palettes import Spectral5
from bokeh.sampledata.autompg import autompg_clean as df
from bokeh.transform import factor_cmap

output_file("bars.html")

df.cyl = df.cyl.astype(str)
df.yr = df.yr.astype(str)

group = df.groupby(('cyl', 'mfr'))

source = ColumnDataSource(group)
index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, 
factors=sorted(df.cyl.unique()), end=1)

p = figure(plot_width=800, plot_height=300, title="Mean MPG by # Cylinders 
           and Manufacturer",
           x_range=group, toolbar_location=None, tools="")

p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=source,
       line_color="white", fill_color=index_cmap, )

p.y_range.start = 0
p.x_range.range_padding = 0.05
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "Manufacturer grouped by # Cylinders"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None

p.add_tools(HoverTool(tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", 
            "@cyl_mfr")]))

show(p)

回答1:

The group = df.groupby(('cyl', 'mfr')) makes a <pandas.core.groupby.DataFrameGroupBy object at 0x0xxx>. If you pass this to a ColumnDataSource, bokeh does a lot of magic, and calculates a lot of statistics already

df.columns
Index(['mpg', 'cyl', 'displ', 'hp', 'weight', 'accel', 'yr', 'origin', 'name', 'mfr'],
source.column_names

['accel_count', 'accel_mean', 'accel_std', 'accel_min', 'accel_25%', 'accel_50%', 'accel_75%', 'accel_max', 'displ_count', 'displ_mean', 'displ_std', 'displ_min', 'displ_25%', 'displ_50%', 'displ_75%', 'displ_max', 'hp_count', 'hp_mean', 'hp_std', 'hp_min', 'hp_25%', 'hp_50%', 'hp_75%', 'hp_max', 'mpg_count', 'mpg_mean', 'mpg_std', 'mpg_min', 'mpg_25%', 'mpg_50%', 'mpg_75%', 'mpg_max', 'weight_count', 'weight_mean', 'weight_std', 'weight_min', 'weight_25%', 'weight_50%', 'weight_75%', 'weight_max', 'yr_count', 'yr_mean', 'yr_std', 'yr_min', 'yr_25%', 'yr_50%', 'yr_75%', 'yr_max', 'cyl_mfr']

  1. the cyl_mfr is the labels of the 2 columns on which you grouped by concatenated. In source this has become a column of tuples

  2. mpg_sum is not calculated. If you cant the sum, you will need to calculate that yourself.