gpt4 book ai didi

python - 如何在 Bokeh 中将 "group by"数据帧绘制为条形图

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:49 24 4
gpt4 key购买 nike

我有一个数据框

     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 的例子都很好

最佳答案

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

关于python - 如何在 Bokeh 中将 "group by"数据帧绘制为条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47301262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com