holoviews /散景-多个堆叠的条形图

时间:2018-10-29 12:36:48

标签: python charts bokeh holoviews

我是全息视图/背景虚化的新手,我对如何构造图表有了一般的感觉,但是我仍然迷失了一些细微差别,并且发现文档中的示例非常有限。

具有分类数据的时间序列,我尝试展示位于彼此下方的一列中的许多堆叠条形图,其中每个图表对应一个'Field',每个图表上的堆叠条形对应{{1 }}。

我正在寻求帮助的问题:


1。我得到的条没有堆叠。如何堆叠它们?

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

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


Field


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

1 个答案:

答案 0 :(得分:1)

这不是您要找的完全是,而是一个开始(我也在学习HoloViews)。

这主要是解决限制示例:http://holoviews.org/reference/elements/bokeh/Bars.html#bokeh-gallery-bars

使用您的数据集,将其处理为以下形式的元组:(字段,类别,百分比_总和):

package MyPackage;
use strict;
use warnings;
use Exporter;
our @ISA = 'Exporter';
our @EXPORT_OK = ...;

然后,绘图:

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

enter image description here