全息视图/散景 - 多个堆叠条形图
holoviews/ bokeh - multiple stacked bar charts
我是全息视图/散景的新手,我对如何构建图表有大致的了解,但我仍然对一些细微差别感到困惑,并且发现文档中的示例非常有限。
拥有分类数据的时间序列,我尝试在彼此下方的一列中显示多个堆叠条形图,其中每个图表对应一个 'Field'
,每个图表上的堆叠条形图对应 Category
.
我正在寻求帮助的问题:
1.我得到的酒吧没有堆叠。如何让它们堆叠起来?
2. 如何改进此图表的构造,以更符合 Python 的方式构建它(在 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')
我是全息视图/散景的新手,我对如何构建图表有大致的了解,但我仍然对一些细微差别感到困惑,并且发现文档中的示例非常有限。
拥有分类数据的时间序列,我尝试在彼此下方的一列中显示多个堆叠条形图,其中每个图表对应一个 'Field'
,每个图表上的堆叠条形图对应 Category
.
我正在寻求帮助的问题:
1.我得到的酒吧没有堆叠。如何让它们堆叠起来?
2. 如何改进此图表的构造,以更符合 Python 的方式构建它(在 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')