简体   繁体   English

holoviews/bokeh - 多个堆积条形图

[英]holoviews/ bokeh - multiple stacked bar charts

I am new to holoviews / bokeh, I got the general feeling how to construct the charts but I am still getting lost with some nuances and find the examples in the documentation very limited.我是全息视图/散景的新手,我对如何构建图表有大致的了解,但我仍然对一些细微差别感到困惑,并且发现文档中的示例非常有限。

Having time series of a categorical data I try to present a number of stack bar charts positioned in a column below each other where each chart corresponds to one 'Field' and stacked bars on each chart correspond to the Category .拥有分类数据的时间序列,我尝试呈现多个堆叠条形图,它们位于彼此下方的一列中,其中每个图表对应一个'Field' ,每个图表上的堆叠条形图对应于Category

Problems I am looking for help with:我正在寻求帮助的问题:


1. The bars I get are not stacked. 1.我得到的酒吧没有堆叠。 How to get them stacked?如何让它们堆叠?

2. How construction of this chart can be improved to build it in a more pythonic way (a loop over the Field )? 2. 如何改进这个图表的构建,以更pythonic 的方式构建它(在Field的循环)?

3. How to configure properly the hover tool for this chart? 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

When I try to add当我尝试添加

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

I get an error:我收到一个错误:

IndexError: list index out of range

This isn't exactly what you're looking for, but maybe a start (I'm learning HoloViews, too).这不完全是您要找的东西,但也许是一个开始(我也在学习 HoloViews)。

This is mostly working off the bars example: http://holoviews.org/reference/elements/bokeh/Bars.html#bokeh-gallery-bars这主要适用于酒吧示例: http : //holoviews.org/reference/elements/bokeh/Bars.html#bokeh-gallery-bars

Using your dataset, manipulate it into tuples of the form (Field, Category, Percentage_Sum):使用您的数据集,将其处理为以下形式的元组 (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)]

Then, plot:然后,情节:

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

在此处输入图片说明

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

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