Search code examples
pythonplotly

adjust plotly subplot xlabel and ylabel distance


How to adjust the subplot graph xlabel and ylabel distance to the graph? Current xlabel and ylabel distance a little bit far away from the graph.

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()

Output: enter image description here


Solution

  • I have not investigated in detail which parameters are affected, but since the structure of the graph is such that the axis labels are grouped into annotations, it is possible to directly update their values to display them anywhere.

    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
    

    enter image description here