这是一个有情节的问题,因为我希望有交互式图表。我正在尝试在彼此的顶部制作两组分组条形图,其中一组未填充但用粗线填充,另一组已填充。
这是我的虚拟数据集:
from numpy import random
import pandas as pd
list = []
for species in ['C1', 'C2', 'C3', 'C4']:
for catalyst in ['CHA', 'SAPO', 'MOF','None']:
for add in [True, False]:
new_data = {'species': species,
'catalyst': catalyst,
'energy': random.uniform(low=0.0, high=1.0, size=None),
'with_additive': add}
list.append(new_data)
df = pd.DataFrame(list)
df
接下来,在情节中,我设法像这样制作条形图
fig = px.bar(df,
x='species',
y='energy',
facet_row ='with_additive',
color = 'catalyst',
barmode ='group',
pattern_shape="with_additive", pattern_shape_sequence=[".", ""]
)
fig
我也可以将它们放在同一个情节中,但随后两组并排结束,如下所示:
fig = px.bar(df,
x='species',
y='energy',
#facet_row ='with_additive',
color = 'catalyst',
barmode ='group',
pattern_shape="with_additive", pattern_shape_sequence=[".", ""]
)
fig
然后我尝试将第二组添加为跟踪,但不要将其分组:
fig = px.bar(df[df.with_additive==True],
x='species',
y='energy',
#facet_row ='with_additive',
color = 'catalyst',
barmode ='group',
)
fig.add_trace(
go.Bar(x=df[df.with_additive==True].species, y=df[df.with_additive==True].energy,
#color = 'surface',
alignmentgroup ='species',
base='overlay',
)
)
所以,我缺少的是:如何获得具有黑线轮廓的集合之一,以及如何将这两个集合完美地叠加在一起,如以下 matplotlib 示例所示:
df_pivot1 = pd.pivot_table(
df[df.with_additive==True],
values="energy",
index="catalyst",
columns="species",
)
df_pivot2 = pd.pivot_table(
df[df.with_additive==False],
values="energy",
index="catalyst",
columns="species",
)
fig, ax = plt.subplots(figsize=(7,5))
df_pivot1.plot(kind="bar", ax=ax)
df_pivot2.plot(kind="bar", edgecolor='black', linewidth=2, ax=ax, fill=False)
handles, labels = ax.get_legend_handles_labels()
handle_list =handles[0:5]
label_list= labels[0:4]
label_list.append('no additive')
plt.legend(handle_list, label_list, loc='upper center', bbox_to_anchor=(0.5, 1.1), ncol=5)