编程语言
首页 > 编程语言> > python-如何在散景中以条形图绘制“分组依据”数据框

python-如何在散景中以条形图绘制“分组依据”数据框

作者:互联网

我有一个数据框

     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不支持Bar.charts,因此任何带有Vbar和Figure的示例都很好

解决方法:

您可以使用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

标签:pandas,bokeh,python
来源: https://codeday.me/bug/20191025/1929513.html