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

如何在Plotly Python中移除冗余的顶部轴标题?

移除Plotly分面箱线图的顶部轴标题

你可以通过两种简单的方式移除这些冗余的顶部轴标题,下面是具体的修改方法:

方法一:批量清空所有x轴标题

直接在代码末尾添加一行,批量清空所有x轴的标题文本,操作最简单:

fig.update_xaxes(title_text="")

如果需要保留底部第一个x轴的标题,可以用下面的针对性修改方式。

方法二:仅移除顶部分面的x轴标题

遍历布局中的各个x轴对象,只清空后续分面(顶部)的x轴标题,保留底部第一个x轴的标题:

# 遍历除第一个x轴外的所有分面x轴,清空标题
for idx in range(1, len(fig.data)):
    xaxis_key = f"xaxis{idx+1}"
    if xaxis_key in fig.layout:
        fig.layout[xaxis_key].title.text = ""

修改后的完整代码

以方法一为例,整合后的完整代码如下:

import plotly.express as px
fig = px.box(DF.melt(), y="value", facet_col="variable", boxmode="overlay", color="variable")
fig.update_yaxes(matches=None)
for i in range(len(fig["data"])):
    yaxis_name = 'yaxis' if i == 0 else f'yaxis{i + 1}'
    fig.layout[yaxis_name].showticklabels = True
#fig.update_layout(showlegend=False)
fig.update_layout(legend = dict(bgcolor = 'white'))
fig.update_layout(plot_bgcolor='white')
fig.update_xaxes(showline=True, linewidth=2, linecolor='black')#, mirror=True)
fig.update_yaxes(showline=True, linewidth=2, linecolor='black')#, mirror=True)
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='gray')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='gray')

# 新增:移除顶部轴标题
fig.update_xaxes(title_text="")

fig.show()

这样就能去掉那些冗余的长轴标题啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 23:27:49