Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

Extra space between subplots and legend

I have a subplot figure with multiple plots and can’t seem to remove the extra space between the plots and legend. Is there any way to remove this extra space? Screenshot and code below.

fig = make_subplots(
    rows=6,
    cols=2,
    column_widths=[0.7, 0.3],
    specs=[[{'rowspan': 2, 'secondary_y': True}, {'rowspan': 3}],
           [None, None],
           [{'rowspan': 2}, None],
           [None, {'rowspan': 3}],
           [{'rowspan': 2}, None],
           [None, None]],
    shared_xaxes=True,
    horizontal_spacing=0.07,
    vertical_spacing=0.02
)

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

fig.update_xaxes(matches='x')
fig.update_xaxes(showgrid=False, row=1, col=1)
fig.update_xaxes(showgrid=False, row=1, col=2)
fig.update_xaxes(showgrid=False, row=3, col=1)
fig.update_xaxes(showgrid=False, row=4, col=2)
fig.update_xaxes(showgrid=False, row=5, col=1)

fig.update_yaxes(title_text='Plant Power (MW)', showgrid=False, zeroline=False, row=1, col=1, secondary_y=False)
fig.update_yaxes(title_text='Cumulative Energy Loss (MWh)', showgrid=False, zeroline=False, row=1, col=1, secondary_y=True)
fig.update_yaxes(title_text='Ambient Temperature (°C)', showgrid=False, zeroline=False, row=1, col=2)
fig.update_yaxes(title_text='Irradiance (W/m<sup>2</sup>)', showgrid=False, zeroline=False, row=3, col=1)
fig.update_yaxes(title_text='Wind Speed (m/s)', showgrid=False, zeroline=False, row=4, col=2)
fig.update_yaxes(title_text='Inverter AC Power (kW)', showgrid=False, zeroline=False, row=5, col=1)

fig.update_layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=20,
        pad=0,
        autoexpand=True
    ),
    paper_bgcolor='rgb(44, 58, 58)',
    plot_bgcolor='rgb(200, 58, 58)',
    font=dict(
        color='rgb(235, 232, 229)',
        family='Arial',
        size=12
    ),
)

config = {
    'displaylogo': False
}

fig.show(config=config)

This kinda works:

fig.update_layout( legend = {"xanchor": "right", "x": 1.08})

It will change the space between the plot and the legend when the window resizes, unfortunately.

There seems to be some issue when calculating the horizontal spacings when secondary_y is True. You can try moving the right-hand-most graph to the side by 6% and increasing the adjacent one via the specs:

fig = make_subplots(
    rows=6,
    cols=2,
    column_widths=[0.7, 0.3],
    specs=[[{'rowspan': 2, 'secondary_y': True,"r":-0.06}, {'rowspan': 3,"l":0.06,"r":-0.06}],
           [None, None],
           [{'rowspan': 2,"r":-0.06}, None],
           [None, {'rowspan': 3,"l":0.06,"r":-0.06}],
           [{'rowspan': 2,"r":-0.06}, None],
           [None, None]],
    shared_xaxes=True,
    horizontal_spacing=0.07,
    vertical_spacing=0.02
)