1
votes

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.

Problems I am looking for help with:


1. The bars I get are not stacked. 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)?

3. How to configure properly the hover tool for this chart?


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
1

1 Answers

1
votes

This isn't exactly what you're looking for, but maybe a start (I'm learning HoloViews, too).

This is mostly working off the bars example: 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):

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')

enter image description here