简体   繁体   English

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

[英]how to plot a “group by” dataframe in Bokeh as Bar chart

i have a dataframe 我有一个数据帧

     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

i have grouped it 我把它分组了

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

Output : 输出:

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

**

How to plot this data in Bokeh? 如何在Bokeh中绘制这些数据? Bar.charts are not supported in latest version of Bokeh , so any example with Vbar and Figure would be great 最新版本的Bokeh不支持Bar.charts,因此任何使用Vbar和Figure的例子都会很棒

You can use visual dodge method : 你可以使用视觉闪避方法

First data preparation: 第一次数据准备:

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)

图形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM