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

如何调整Plotly子图坐标轴标签与图表的间距?

解决Plotly子图坐标轴标签与图表间距过大的问题

问题分析

你在make_subplots中设置了全局的x_title和y_title,但默认的标签与图表间距偏大。单独设置子轴的title_standoff不会影响全局标签的位置,需要直接调整全局标签的间距参数。

修改方案

在fig.update_layout()中添加xaxis_title_standoff和yaxis_title_standoff参数,设置较小数值缩小标签与图表的距离;同时可调整底部、左侧边距配合优化整体布局。

修改后的完整代码

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

def save_fig(fig,pngname):
    width = 800
    height = 400
    fig.write_image(pngname,format="png", width=width, height=height, scale=1)
    print("[[%s]]"%pngname)
    #fig.show()
    return

def plot():
    fig = make_subplots(
        rows=2, cols=1, 
        shared_xaxes=True,
        vertical_spacing = 0.02,
        x_title='x',
        y_title='y',                
        specs=[[{"type": "xy"}],
               [{"type": "xy"}]],
    )

    fig.add_trace(go.Bar(y=[2, 3, 1]),
              row=1, col=1)

    fig.add_trace(go.Scatter(x=[1,2,4],y=[3,2,5]),
              row=2, col=1)

    fontsize=10
    xpading=.05
    fig.update_layout(
        margin=dict(l=40,t=40,r=10,b=30),  # 缩小左侧和底部边距适配标签间距
        plot_bgcolor='#ffffff',
        paper_bgcolor='#ffffff',        
        title_x=0.5,
        showlegend=True,
        legend=dict(x=.02,y=1.05),        
        barmode='group',
        bargap=0.05,
        bargroupgap=0.0,
        font=dict(
            family="Courier New, monospace",
            size=fontsize,
            color="black"
        ),
        # 核心:调整全局坐标轴标签与图表的间距
        xaxis_title_standoff=5,
        yaxis_title_standoff=5,
        xaxis=dict(
            visible=True,            
            title_standoff=1,
            tickangle=-15,            
            showline=True,
            linecolor='black',
            color='black',
            linewidth=.5,
            ticks='outside',
            showgrid=True,
            gridcolor='grey',
            gridwidth=.5,
            griddash='solid',
        ),
        yaxis=dict(
            title_standoff=1,
            showline=True,
            linecolor='black',
            color='black',
            linewidth=.5,            
            showgrid=True,
            gridcolor='grey',
            gridwidth=.5,
            griddash='solid',
            zeroline=True,
            zerolinecolor='grey',
            zerolinewidth=.5,
            showticklabels=True,
        ),        
        xaxis2=dict(
            title_standoff=1,
             tickangle=-15,            
            showline=True,
            linecolor='black',
            color='black',
            linewidth=.5,
            ticks='outside',
             showgrid=True,
            gridcolor='grey',
            gridwidth=.5,
            griddash='solid',
        ),
        yaxis2=dict(
            title_standoff=1,
             showline=True,
            linecolor='black',
            color='black',
            linewidth=.5,            
             showgrid=True,
            gridcolor='grey',
            gridwidth=.5,
            griddash='solid',
            zeroline=True,
            zerolinecolor='grey',
            zerolinewidth=.5,
            showticklabels=True,
         ),        
    )
    
    save_fig(fig,"./demo.png")
    return

plot()

关键调整说明

  • xaxis_title_standoff=5和yaxis_title_standoff=5:直接控制全局坐标轴标签与图表的距离,数值越小间距越近,可根据实际显示效果微调。
  • 缩小margin的l和b值:避免标签被边距截断,同时让整体布局更紧凑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 16:40:01