gpt4 book ai didi

python - Holoviews/bokeh - 多个堆叠条形图

转载 作者:行者123 更新时间:2023-12-01 08:52:01 25 4
gpt4 key购买 nike

我是 Holoviews/bokeh 的新手,我对如何构建图表有了大致的了解,但我仍然迷失在一些细微差别中,并且发现文档中的示例非常有限。

有了分类数据的时间序列,我尝试呈现多个堆叠条形图,这些条形图位于彼此下方的一列中,其中每个图表对应于一个“Field”,并且每个图表上的堆叠条形图对应到类别

我正在寻求帮助的问题:

<小时/>

<强>1。我得到的酒吧没有堆叠。如何让它们堆叠起来?

2. 如何改进此图表的构造,以更加 Pythonic 的方式构建它(在 Field 上循环)?

3. 如何正确配置此图表的悬停工具?


df_example =   pd.DataFrame(data= [('2018-01-01','A','F1',0.05),('2018-01-01','B','F1',0.15),('2018-01-01','C','F1',0.12),
('2018-01-01','A','F2',0.16),('2018-01-01','B','F2',0.11),('2018-01-01','C','F2',0.04),
('2018-01-01','A','F3',0.08),('2018-01-01','B','F3',0.07),('2018-01-01','C','F3',0.14),
('2018-01-01','A','F4',0),('2018-01-01','B','F4',0),('2018-01-01','C','F4',0),

('2018-01-02','A','F1',0.05),('2018-01-02','B','F1',0.05),('2018-01-02','C','F1',0.19),
('2018-01-02','A','F2',0.15),('2018-01-02','B','F2',0.04),('2018-01-02','C','F2',0.0003),
('2018-01-02','A','F3',0.12),('2018-01-02','B','F3',0.25),('2018-01-02','C','F3',0.1),
('2018-01-02','A','F4',0), ('2018-01-02','B','F4',0), ('2018-01-02','C','F4',0),

('2018-01-03','A','F1',0.08),('2018-01-03','B','F1',0.28),('2018-01-03','C','F1',0.12),
('2018-01-03','A','F2',0.06),('2018-01-03','B','F2',0.08),('2018-01-03','C','F2',0.04),
('2018-01-03','A','F3',0.06),('2018-01-03','B','F3',0.05),('2018-01-03','C','F3',0.14),
('2018-01-03','A','F4',0), ('2018-01-03','B','F4',0), ('2018-01-03','C','F4',0),

('2018-01-04','A','F1',0.21),('2018-01-04','B','F1',0.09),('2018-01-04','C','F1',0.03),
('2018-01-04','A','F2',0.14),('2018-01-04','B','F2',0.15),('2018-01-04','C','F2',0.0002),
('2018-01-04','A','F3',0.15),('2018-01-04','B','F3',0.08),('2018-01-04','C','F3',0.14),
('2018-01-04','A','F4',0),('2018-01-04','B','F4',0),('2018-01-04','C','F4',0),]
,columns=['Date','Category','Field','Percentage'])

df_example

index Date Category Field Percentage
0 2018-01-01 A F1 0.050
1 2018-01-01 B F1 0.150
2 2018-01-01 C F1 0.120
3 2018-01-01 A F2 0.160
4 2018-01-01 B F2 0.110
5 2018-01-01 C F2 0.040
6 2018-01-01 A F3 0.080
7 2018-01-01 B F3 0.070
8 2018-01-01 C F3 0.140
9 2018-01-01 A F4 0.000
10 2018-01-01 B F4 0.000
11 2018-01-01 C F4 0.000
12 2018-01-02 A F1 0.050
...


Fields = pd.Series(['F1','F2','F3','F4'])

data_0 = df_example[df_example['Field'] == str(Fields[0]) ]
data_1 = df_example[df_example['Field'] == str(Fields[1]) ]
data_2 = df_example[df_example['Field'] == str(Fields[2]) ]
data_3 = df_example[df_example['Field'] == str(Fields[3]) ]


b_0 = hv.Bars(data_0, ['Date','Field','Category'],['Percentage'],
group = str(Fields[0]))
b_1 = hv.Bars(data_1, ['Date','Field','Category'],['Percentage'],
group = str(Fields[1]))
b_2 = hv.Bars(data_2, ['Date','Field','Category'],['Percentage'],
group = str(Fields[2]))
b_3 = hv.Bars(data_3, ['Date','Field','Category'],['Percentage'],
group = str(Fields[2]))

layout = hv.Layout(b_0 + b_1 + b_2 + b_3).cols(1)
layout

当我尝试添加

%opts Bars [stack_index = 0  show_legend=True tools=['hover']] 

我收到错误:

IndexError: list index out of range

最佳答案

这并不是您正在寻找的东西,但也许是一个开始(我也在学习 HoloViews)。

这主要是在酒吧示例:http://holoviews.org/reference/elements/bokeh/Bars.html#bokeh-gallery-bars

使用您的数据集,将其处理为以下形式的元组(Field、Category、Percentage_Sum):

sums = df_example.groupby(['Field','Category']).sum().reset_index()
sums.head()

Field Category Percentage
0 F1 A 0.39
1 F1 B 0.57
2 F1 C 0.46
3 F2 A 0.51
4 F2 B 0.38

tuples = [tuple(x) for x in sums.values]
tuples[:5]

[('F1', 'A', 0.39),
('F1', 'B', 0.5700000000000001),
('F1', 'C', 0.45999999999999996),
('F2', 'A', 0.51),
('F2', 'B', 0.38)]

然后,绘制:

%%opts Bars [stack_index='Category' tools=['hover'] width=400]
hv.Bars(tuples, ['Field', 'Category'], 'Percent_Sum')

enter image description here

关于python - Holoviews/bokeh - 多个堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045664/

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