ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-25 14:55:12  阅读:263  来源: 互联网

标签:pandas bokeh 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有