You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Python Plotly.express中子图图例项重复问题

问题描述

我正在使用Plotly.express生成包含3个子图的图表,每个图例项会在三个子图中重复显示(每个子图对应一个图例条目)。点击图例项时,所有对应子图中的线条会同步开关(无需独立控制)。由于图例为可变的文件名,无法使用固定图例标题,请问能否将每个图例项简化为仅显示一次?可忽略代码中的'Ambient'部分。

代码示例

fig = make_subplots(rows=1, cols=3,
                    column_titles=['Velocity m/s', 'Temperature C',
                                   'Salinity '],
                    row_titles=['Depth m', 'Depth m'],
                    shared_yaxes=True)

fig1 = px.line(dfs, x='Sound Velocity', y='Depth',
               color='File')
fig2 = px.line(dfs, x='Temperature', y='Depth',
               color='File')
fig3 = px.line(dfs, x='Salinity', y='Depth',
               color='File')

dips = dfs['File'].nunique()  # Count number of different files

for dip in range(dips):
    fig.add_trace(fig1['data'][dip], row=1, col=1)

for dip in range(dips):
    fig.add_trace(fig2['data'][dip], row=1, col=2)

for dip in range(dips):
    fig.add_trace(fig3['data'][dip], row=1, col=3)

fig.update_layout(template="plotly_dark")
fig.add_trace(go.Scatter(y=[z_min, z_max], x=[rv_min, v_max],
                         line=dict(color='#D7CECE', width=2,
                         dash='dash'), name="Ambient"))

数据样例(跨两个文件SVP-3 & SVP-4)

Date/Time   Depth   Sound Velocity  Pressure    Temperature Conductivity    Salinity    Density File
1377    22/10/2023 05:17    -2.719  1545.445    2.719   29.25   59.854  36.58   1023.179    SVP-3@YOTI 2-DEL.txt
1378    22/10/2023 05:17    -2.092  1545.432    2.092   29.248  59.854  36.582  1023.178    SVP-3@YOTI 2-DEL.txt
1379    22/10/2023 05:17    -1.592  1545.418    1.592   29.248  59.852  36.581  1023.175    SVP-3@YOTI 2-DEL.txt
1380    22/10/2023 05:17    -1.178  1545.41 1.178   29.247  59.848  36.579  1023.172    SVP-3@YOTI 2-DEL.txt
1381    22/10/2023 05:17    -0.691  1545.408    0.691   29.246  59.854  36.584  1023.174    SVP-3@YOTI 2-DEL.txt
1382    22/10/2023 05:17    -0.171  1545.415    0.171   29.247  59.844  36.576  1023.166    SVP-3@YOTI 2-DEL.txt
1383    24/10/2023 15:59    -1.39   1543.341    1.39    29.397  56.71   34.315  1021.423    SVP-4@YOTI 2-DEL.txt
1384    24/10/2023 15:59    -1.585  1543.34 1.585   29.397  56.708  34.314  1021.423    SVP-4@YOTI 2-DEL.txt
1385    24/10/2023 15:59    -2.261  1543.356    2.261   29.395  56.704  34.312  1021.425    SVP-4@YOTI 2-DEL.txt
1386    24/10/2023 15:59    -2.788  1543.38 2.788   29.396  56.71   34.315  1021.429    SVP-4@YOTI 2-DEL.txt
1387    24/10/2023 15:59    -3.173  1543.383    3.173   29.396  56.712  34.317  1021.432    SVP-4@YOTI 2-DEL.txt
1388    24/10/2023 15:59    -3.591  1543.373    3.591   29.395  56.71   34.316  1021.434    SVP-4@YOTI 2-DEL.txt
1389    24/10/2023 15:59    -4.095  1543.358    4.095   29.39   56.706  34.316  1021.438    SVP-4@YOTI 2-DEL.txt
1390    24/10/2023 16:00    -4.654  1543.339    4.654   29.382  56.696  34.315  1021.442    SVP-4@YOTI 2-DEL.txt
1391    24/10/2023 16:00    -5.362  1543.197    5.362   29.35   56.64   34.3    1021.444    SVP-4@YOTI 2-DEL.txt

问题解决说明

问题已解决,感谢EricLavault的解答。更新后的图表完全符合需求,代码更简洁,采用了提供的方案,包括缩小trace_groupgap的建议。


内容的提问来源于stack exchange,提问作者WillH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 03:40:01