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

Plotly Graph Objects子图配置双X轴:仅顶部子图生效求助

问题:Plotly子图仅顶部显示双X轴,其余子图无法实现

使用Plotly Graph Objects绘制2行1列的子图,需求是每个子图同时显示顶部和底部X轴,但目前只有顶部子图能实现双X轴效果,下方子图的顶部X轴不显示。

原代码如下:

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

label_par = [r'$x^2$ \n [cm]', r'$x^3$ \n [cm]']

fig = make_subplots(rows=2, cols=1, vertical_spacing=0.01,
                    specs=[[{"secondary_y": True}], [{"secondary_y": True}]])
# panel(a)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16], 
showlegend=False, text=["(a)"], textposition='top left'), row=1, col=1, secondary_y=False)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16], 
showlegend=False), row=1, col=1, secondary_y=True)
# panel (b)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 8, 27, 64], 
showlegend=False,text=["(a)"], textposition='top left'), row=2, col=1, secondary_y=False)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 8, 27, 64], 
showlegend=False), row=2, col=1, secondary_y=True)
fig.update_layout(plot_bgcolor='rgba(0, 0, 0, 0)')
fig.update_yaxes(linecolor='black', secondary_y=False, showgrid=False)
fig.update_yaxes(linecolor='black', secondary_y=True, showgrid=False)
fig.update_yaxes(minor_ticks="inside")
fig.update_xaxes(ticks="inside")
fig['layout']['yaxis1']['title'] = label_par[0]
fig['layout']['yaxis3']['title'] = label_par[1]
fig.update_layout(xaxis3={'anchor': 'y1', 'overlaying': 'x', 'side': 'top'},
                  xaxis4={'anchor': 'y3', 'overlaying': 'x2', 'side': 'top'})
fig.data[0].update(xaxis='x3')
fig.data[1].update(xaxis='x4')
fig.update_xaxes(linecolor='black', showticklabels=False, showgrid=False)
fig.update_xaxes(showticklabels=True, row=2, col=1)
fig.write_image("/home/dhanya/Desktop/compiled.pdf", width=600, height=600)

效果说明:仅顶部子图(panel(a))显示了顶部和底部X轴,底部子图(panel(b))仅显示底部X轴,顶部X轴未出现。


解决方案

问题出在X轴关联绑定错误、全局设置覆盖刻度显示这两点,调整后即可实现每个子图的双X轴效果:

修正后的代码如下:

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

label_par = [r'$x^2$ \n [cm]', r'$x^3$ \n [cm]']

fig = make_subplots(rows=2, cols=1, vertical_spacing=0.01,
                    specs=[[{"secondary_y": True}], [{"secondary_y": True}]])
# panel(a)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16], 
showlegend=False, text=["(a)"], textposition='top left'), row=1, col=1, secondary_y=False)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16], 
showlegend=False), row=1, col=1, secondary_y=True)
# panel (b)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 8, 27, 64], 
showlegend=False,text=["(b)"], textposition='top left'), row=2, col=1, secondary_y=False)
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 8, 27, 64], 
showlegend=False), row=2, col=1, secondary_y=True)

# 布局与轴样式设置
fig.update_layout(plot_bgcolor='rgba(0, 0, 0, 0)')

# Y轴样式统一设置
fig.update_yaxes(linecolor='black', showgrid=False, minor_ticks="inside")
fig['layout']['yaxis1']['title'] = label_par[0]
fig['layout']['yaxis3']['title'] = label_par[1]

# 配置每个子图的顶部X轴,直接设置显示属性
fig.update_layout(
    xaxis3={'anchor': 'y1', 'overlaying': 'x', 'side': 'top', 'linecolor': 'black', 'ticks': 'inside', 'showticklabels': True},
    xaxis4={'anchor': 'y3', 'overlaying': 'x2', 'side': 'top', 'linecolor': 'black', 'ticks': 'inside', 'showticklabels': True}
)

# 绑定每个子图的所有trace到对应顶部X轴
fig.data[0].update(xaxis='x3')
fig.data[1].update(xaxis='x3')
fig.data[2].update(xaxis='x4')
fig.data[3].update(xaxis='x4')

# 设置底部X轴样式
fig.update_xaxes(linecolor='black', ticks="inside", showgrid=False, showticklabels=True)

fig.write_image("/home/dhanya/Desktop/compiled.pdf", width=600, height=600)

关键调整点:

  1. 给底部子图的两个trace都绑定顶部X轴x4,确保子图关联到顶部X轴
  2. 修正xaxis4的锚点为y3(底部子图主Y轴),覆盖目标改为x2(底部子图默认X轴)
  3. 直接在顶部X轴的配置中开启刻度显示,避免全局设置隐藏所有X轴刻度
  4. 同一子图的多个trace绑定到同一个顶部X轴,保证轴的统一性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 03:10:12