如何为散景vplot创建多索引?

时间:2017-10-18 12:41:50

标签: pandas sorting bar-chart bokeh pandas-groupby

当处理需要groupby的散景vpl时,我遇到了问题。问题是,vplots需要 x_range 由字符串组成,如果我在df.groupby((代码,年,月)之前转换列(月,年)),那么10,11和12将在1之后放置。

group = df2.groupby(('Code' ,'Year', 'Month'))

source = ColumnDataSource(group)

index_cmap  = factor_cmap('Code_Year_Month', palette=['red', 'green', 
'navy'], factors=sorted(df2.Code.unique()),end=1)

p = figure(plot_width=800, plot_height=300, title="Test",
    x_range=group, toolbar_location=None, tools="")

p.vbar(x='Code_Year_Month', top='Column_mean', width=1, source=source,
   line_color="white", fill_color=index_cmap, fill_alpha = 0.5)

p.add_tools(HoverTool(tooltips=[("column:", "@column_mean"),\
                            ("Code: ", "@Code"),\
                            ("Year: ", "@Year"),\
                            ("Month: ", "@CMonth")]))
p.y_range.start = 0
p.x_range.range_padding = 0.05
p.xgrid.grid_line_color = None
p.xaxis.axis_label = ".... X"
p.yaxis.axis_label = "...."
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None

grid = gridplot(p, ncols=1, plot_width=800, plot_height=300, sizing_mode= 
'stretch_both', toolbar_location = 'right')

show(grid)

当我尝试使用x_range时,当某些列为浮点数时,这是ValueError。

ValueError: expected an element of either Seq(String), Seq(Tuple(String, String)) or Seq(Tuple(String, String, String)), got [('P42F', 2015, 7), ... 

如何从以下位置对多索引进行排序:

group = df2.groupby(('Code' ,'Year', 'Month') 

什么时候所有列都需要是字符串?

0 个答案:

没有答案