如何在Bokeh中绘制“分组依据”数据框作为条形图

时间:2017-11-15 07:03:08

标签: python pandas bokeh

我有一个数据框

     suite_name  fail  Pass      Report_datetime
0   VOLTE-VOLTE     5     7  2017-11-14 00:00:00
1   VOLTE-VOLTE     5     7  2017-11-11 00:00:00
2   VOLTE-VOLTE     5     7  2017-11-10 00:00:00
3   VOLTE-VOLTE     5     7  2017-11-09 00:00:00
4   VOLTE-VOLTE     5     7  2017-11-14 00:00:00
5   VOLTE-VOLTE     5     7  2017-11-14 00:00:00

我已将其分组

g1=df.groupby( [ 'Report_datetime'] ).sum()
print g1

输出

Report_datetime         fail        Pass       
2017-11-14 00:00:00     5     7
2017-11-11 00:00:00     5     7
2017-11-10 00:00:00     5     7
2017-11-10 00:00:00     5     7

**

如何在Bokeh中绘制这些数据? 最新版本的Bokeh不支持Bar.charts,因此任何使用Vbar和Figure的例子都很棒

1 个答案:

答案 0 :(得分:4)

您可以使用visual dodge method

首次数据准备:

g1 = df.groupby('Report_datetime', as_index=False).sum()
print (g1)
  Report_datetime  fail  Pass
0      2017-11-09     5     7
1      2017-11-10     5     7
2      2017-11-11     5     7
3      2017-11-14    15    21

#convert datetimes to strings
g1['Report_datetime'] = g1['Report_datetime'].dt.strftime('%Y-%m-%d')
#convert dataframe to dict
data = g1.to_dict(orient='list')
dates = g1['Report_datetime'].tolist()
from bokeh.core.properties import value
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import dodge


output_file("dodged_bars.html")

source = ColumnDataSource(data=data)


#get max possible value of plotted columns with some offset
p = figure(x_range=dates, y_range=(0, g1[['fail','Pass']].values.max() + 3), 
           plot_height=250, title="Report",
           toolbar_location=None, tools="")

p.vbar(x=dodge('Report_datetime', -0.25, range=p.x_range), top='fail', width=0.4, source=source,
       color="#c9d9d3", legend=value("fail"))

p.vbar(x=dodge('Report_datetime',  0.25,  range=p.x_range), top='Pass', width=0.4, source=source,
       color="#718dbf", legend=value("Pass"))


p.x_range.range_padding = 0.1
p.xgrid.grid_line_color = None
p.legend.location = "top_left"
p.legend.orientation = "horizontal"

show(p)

graph