adjust plotly subplot xlabel and ylabel distance

问题描述 投票:0回答:1

如何调整subplot图的xlabel和ylabel到图的距离?当前的 xlabel 和 ylabel 距离图有点远。

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=50,t=40,r=10,b=40),
        plot_bgcolor='#ffffff',#'rgb(12,163,135)',
        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=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',#'dot',            
        ),
        yaxis=dict(
            title_standoff=1,
            showline=True,
            linecolor='black',
            color='black',
            linewidth=.5,            
            showgrid=True,
            gridcolor='grey',
            gridwidth=.5,
            griddash='solid',#'dot',
            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',#'dot',            
        ),
        yaxis2=dict(
            title_standoff=1,
             showline=True,
            linecolor='black',
            color='black',
            linewidth=.5,            
             showgrid=True,
            gridcolor='grey',
            gridwidth=.5,
            griddash='solid',#'dot',
            zeroline=True,
            zerolinecolor='grey',
            zerolinewidth=.5,
            showticklabels=True,
         ),        
    )
    
    save_fig(fig,"./demo.png")
    return

plot()

输出:

python plotly
1个回答
0
投票

我没有详细调查哪些参数受到影响,但由于图形的结构是轴标签被分组为注释,因此可以直接更新它们的值以在任何地方显示它们。

def plot():
    ...
    save_fig(fig,"./demo.png")
    fig.update_annotations(selector={"text":"x"}, yshift=-10, x=0.5)
    fig.update_annotations(selector={"text":"y"}, xshift=-10, y=0.5)
    #fig.show()
    print(fig.layout)
    return

© www.soinside.com 2019 - 2024. All rights reserved.