情节:如何使分组的条形图相互重叠?

Plotly: How to make grouped bargraphs on top of each other?

这是一个情节性的问题,因为我希望有交互式图表。 我正在尝试制作两组相互重叠的分组条形图,其中一组未填充但用粗线填充,另一组已填充。

这是我的虚拟数据集:

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

接下来,在 plotly 中,我设法像这样一个接一个地制作条形图

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)

你可以这样做:

1.建立两个数字fig1=px.bar()fig2=px.bar()

2.fig2 进行必要的更改,使痕迹突出,没有颜色和黑线:

 fig2.update_traces(marker_color = 'rgba(0,0,0,0)', marker_line_color = 'black')

3.fig2 的跟踪名称更改为:

fig2.for_each_trace(lambda t: t.update(name = t.name + ' No additives'))

3.设置新图fig=go.Figure(fig1.data)

4. 使用 fig.add_traces(fig2.data)

fig2 添加已编辑的轨迹到 fig

就是这样:

情节

代码:

from numpy import random
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

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)            

fig1 = px.bar(df[df.with_additive==True],
             x='species',
             y='energy',
             facet_row ='with_additive',
             color = 'catalyst',
             barmode ='group',
             # pattern_shape="with_additive", pattern_shape_sequence=[".", ""]
            )

fig2 = px.bar(df[df.with_additive==False],
             x='species',
             y='energy',
             facet_row ='with_additive',
             color = 'catalyst',
             barmode ='group',
            )

fig2.update_traces(marker_color = 'rgba(0,0,0,0)', marker_line_color = 'black')
fig2.for_each_trace(lambda t: t.update(name = t.name + ' No addititves'))

fig = go.Figure(fig1.data)
fig.add_traces(fig2.data)
fig.show()