我正在编写一个用于游戏分析的简单工具,但是在Bokeh图中显示嵌套数据框时遇到了问题。 当我选择基本类别参数时,例如“按游戏类型排序”
total_players
type
DM 184
TDM 52
<class 'pandas.core.frame.DataFrame'>
图形显示正确-https://imgur.com/kTSsvHl
但是当我尝试选择嵌套类别时,例如“按地图分类游戏类型”并按属性“和”计数
total_players
type map
DM Cinema 116
Construction 14
Lighthouse 10
Ship 44
TDM Cinema 38
Lighthouse 8
Ship 6
<class 'pandas.core.frame.DataFrame'>
散景图将为空,无法显示
我想要得到这样的东西:https://imgur.com/dP7XBj5,但是使用'sum'方法进行汇总。 资料集: Dataset
代码:
第一个“ if”块中的问题
def gamerounds_graph_create(category, attribute, sort_by):
current_feature_set = category.split("_") # transform from "cat1_cat2" label to cat list['cat1', 'cat2'] need for grouping
df = pd.read_sql(GameRounds.select().sql()[0], db.obj.connection()) # 0 - raw sql #TODO split data by chunks
df.to_csv('game_rounds.csv')
if attribute != 'total' and sort_by == '_count':
"""
Problem in this 'if' block.
"""
group = df.groupby(current_feature_set, as_index=True).agg({attribute: "sum"})
source = ColumnDataSource().from_df(group) #Convert df to bokeh format
ranges = df[current_feature_set[0]].unique()
del df #Free RAM, df can be big
plot = figure(plot_height=400, plot_width=1000, x_range=ranges,
title="{attribute} by {category}".format(attribute=attribute, category=category))
plot.vbar(source=source, x=category, top=attribute, width=0.8)
else:
group = df.groupby(current_feature_set)
factors = df[current_feature_set[0]].unique()
print(type(group))
del df #Free RAM, df can be big
total_factors = len(factors)
if total_factors < 3 or total_factors > 12:
total_factors = 12
palette = palette_dict['Set3'][total_factors]
source = ColumnDataSource(group) #Convert df to bokeh format
plot = figure(plot_height=400, plot_width=1000, x_range=group,
title="{attribute} by {category}".format(attribute=attribute, category=category))
factors_map = factor_cmap(category, factors=factors, palette=palette) #broken for nested categories
if attribute == 'total':
attribute = 'id' #Categories can be counted by numeric attribute only - id
sort_by = '_count'
plot.vbar(source=source, x=category, top=attribute.__add__(sort_by), width=0.8,
fill_color = factors_map
) # inbuilt parameter of ColumnDataSource, already computed
plot.legend.orientation = 'horizontal'
return components(plot)
P.S如何为嵌套类别的vbar添加颜色?