Plotly:将一条迹线关联到多个图例组

问题描述 投票:0回答:1

我正在使用plotly和python 3.9在绘图上显示痕迹。

我的人物有多个子图:

fig = make_subplots(rows=numberOfRows, cols=1, 
                    horizontal_spacing = 0.04,
                    vertical_spacing=0.04,
                    shared_xaxes=True)
for key, value in filtered_dict.items():
    fig.add_trace(go.Scatter(x=eval(data_to_plot_X)[key], y=eval(data_to_plot_Y)[key], hovertemplate=hoverY1,
                                     marker=dict(color=colorList[c]), name=key, legendgroup=key),
                                      row=1, col=1,)
    fig.add_trace(go.Scatter(x=eval(data_to_plot_X)[key], y=eval(data_to_plot_Y2)[key], hovertemplate=hoverY2, 
                                         marker=dict(color=colorList[c]), name=key, legendgroup=key, showlegend=False),
                                          row=2, col=1,)
fig.update_layout(hovermode="x")
fig.show()    

因此,对于每个键(对应于测试),每个子图上都有一条迹线,并且每个键只有 1 个图例。借助 legendgroup,我只需单击一下即可激活或停用所有子图中某个键的跟踪。

现在,我的字典中有很多键,对应于多组测试。我希望通过单击图例,我还可以停用或激活一组测试的所有痕迹。

但是到目前为止我还没有成功,似乎每个痕迹只能有1个传奇组。有办法实现吗?谢谢

python-3.x plotly legend
1个回答
0
投票

在 Plotly 中,您只能将迹线分配给一个图例组,但您可以通过使用带有 Plotly 按钮的嵌套图例组来实现您的目标。这允许您按多个标准对轨迹进行分组并集体切换它们。

import plotly.graph_objects as go
from plotly.subplots import make_subplots


data_to_plot_X = {'test1': [1, 2, 3], 'test2': [1, 2, 3], 'test3': [1, 2, 3]}
data_to_plot_Y = {'test1': [2, 4, 6], 'test2': [3, 6, 9], 'test3': [4, 8, 12]}
data_to_plot_Y2 = {'test1': [1, 3, 5], 'test2': [2, 4, 6], 'test3': [3, 5, 7]}

group_dict = {
    'group1': ['test1', 'test2'],
    'group2': ['test3']
}

colorList = ['blue', 'green', 'red']

fig = make_subplots(rows=2, cols=1, shared_xaxes=True)

c = 0
for key in data_to_plot_X.keys():
    fig.add_trace(go.Scatter(
        x=data_to_plot_X[key], 
        y=data_to_plot_Y[key], 
        hovertemplate='Y1', 
        marker=dict(color=colorList[c]), 
        name=key, 
        legendgroup=key  # Control visibility by key across subplots
    ), row=1, col=1)

    fig.add_trace(go.Scatter(
        x=data_to_plot_X[key], 
        y=data_to_plot_Y2[key], 
        hovertemplate='Y2', 
        marker=dict(color=colorList[c]), 
        name=key, 
        legendgroup=key, 
        showlegend=False  # Hide duplicate legends in the second subplot
    ), row=2, col=1)
    
    c += 1

buttons = []
for group_name, tests in group_dict.items():
    # Create a list of visibility booleans for traces in the group
    visibility = []
    for key in data_to_plot_X.keys():
        if key in tests:
            visibility += [True, True]  # Show both traces (one for each subplot)
        else:
            visibility += [False, False]  # Hide both traces
    buttons.append(dict(label=group_name,
                        method='update',
                        args=[{'visible': visibility}]))

# Add buttons to the layout
fig.update_layout(
    updatemenus=[dict(
        type="buttons",
        direction="down",
        buttons=buttons,
        showactive=True
    )],
    hovermode="x"
)

fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.