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

如何移除plotly.subplots中第二个子图的'trace 1'标识

移除Plotly子图中箱线图的"trace 1"标识

要删除第二个子图(箱线图)上显示的"trace 1"文字,只需在创建箱线图的trace时将name属性设为空字符串,覆盖默认生成的trace名称即可。

修改后的完整代码如下:

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np

subplot_titles = ['Histogram', 'Boxplot']
rows = 1
columns = 2
xaxis_title="full_sq"
yaxis_title="Value" 

fig = make_subplots(
    rows=rows, 
    cols=columns, 
    subplot_titles=['Histogram', 'Boxplot']
)

trace0 = go.Histogram(
    x=Df["full_sq"]
)

# 清空箱线图的trace名称,消除"trace 1"标识
trace1 = go.Box(
    x=Df["full_sq"], 
    marker_color = 'lightseagreen',
    name=''
)

list_trace = [trace0, trace1]

for i, col in enumerate(subplot_titles):
    r = int(np.ceil(((i+1)/columns)))
    c = i%2+1
    fig.add_trace(list_trace[i], row=r, col=c)
    fig.update_xaxes(title_text=xaxis_title, row=r, col=c)
    fig.update_yaxes(title_text=yaxis_title, row=r, col=c)

fig.update_layout(height = 400, width = 700, showlegend = False)
fig.show()

另外注意:你代码中添加的fig.add_annotation(name = False)属于无效操作,对移除trace名称没有帮助,可以直接删除这行代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 13:50:24