问题描述
问题
以下代码来自bokeh文档中的分组vbar图表示例.在这个例子中我有些不明白.
Below code is grouped vbar chart example from bokeh documentation.There are something i can't understand on this example.
-
"cyl_mfr"来自于factor_cmap()和vbar()中?
Where 'cyl_mfr' is come from in factor_cmap() and vbar()?
'mpg_mean',它是否在计算'mpg'列的平均值?如果那样的话 为什么"mpg_sum"不起作用?
'mpg_mean' , is it calculating the mean of 'mpg' column? if then, why 'mpg_sum' doesn't work?
我想像这样的例子制作自己的vbar图表.
I want to make my own vbar chart like this example.
代码
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)
推荐答案
group = df.groupby(('cyl', 'mfr'))
产生<pandas.core.groupby.DataFrameGroupBy object at 0x0xxx>
.如果将其传递给ColumnDataSource
,则bokeh会产生很多魔力,并且已经计算出很多统计数据
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
-
cyl_mfr
是您按串联分组的2列的标签.在source
中,它已成为一列元组
the
cyl_mfr
is the labels of the 2 columns on which you grouped by concatenated. Insource
this has become a column of tuples
mpg_sum
未计算.如果您不能计算总和,则需要自己计算.
mpg_sum
is not calculated. If you cant the sum, you will need to calculate that yourself.
这篇关于如何将bokeh vbar chart参数与groupby对象一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!