1

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

这是我的虚拟数据集:

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

结果: plotly bragraphs,两个单独的图表

我也可以将它们放在同一个情节中,但随后两组并排结束,如下所示:

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)

根据需要,但使用 matplotlib

4

1 回答 1

0

你可以这样做:

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.将编辑后的轨迹添加figfig2使用fig.add_traces(fig2.data)

就是这样:

阴谋

在此处输入图像描述

代码:

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()
于 2022-01-27T13:47:52.947 回答