Center subplots in a larger width figure

Hello,

I’m creating multiple figures with 2 subplots, and a fixed figsize.
While specifying the horizontal_spacing, if the combined subplots are thinner than my fig width, the space between them becomes wider and the subplots aren’t centered anymore.
I’m trying to keep them centered and close together.

Test code :

import numpy as np
from plotly.subplots import make_subplots
from plotly.graph_objs import Contour, Scatter

figure = make_subplots(rows=1, cols=2, shared_yaxes=True, column_widths=[0.2, 0.8], horizontal_spacing=0.04)

t = np.linspace(0, 2*np.pi, 100)

trace_1 = Scatter(x = np.cos(t),
                  y = t,
                  mode = 'lines')

trace_2 = Contour(z=[[10, 12.5, 20],
                         [5.625, 8.125, 15.625],
                         [2.5, 5., 12.5],
                         [0.625, 3.125, 10.625],
                         [0, 2.5, 10]],
                      x=[-9, -7 , -5], # horizontal axis
                      y=[0, 1, 4, 5, 7], # vertical axis
                      contours_coloring = 'heatmap',
                      contours_showlabels = True)

figure.add_trace(trace_1, row=1, col=1)
figure.add_trace(trace_2, row=1, col=2)

figure.update_layout(title_text = 'Title<br><span style="font-size:14px">Subtitle</span>',
                     xaxis1 = dict(title='x1', constrain='domain'),
                     xaxis2 = dict(title='x2', constrain='domain'),
                     yaxis = dict(title='y', constrain='domain', scaleanchor="x2", scaleratio=1))

figure.show(renderer='png', width=1080, height=720)

Output :

Wanted :

Do you have any advice to achieve this ?

Thanks !

Hey @GeminyCriquet ,

Welcome to the forum.

This is trial and error. I simply added another column and tinkered column widths.

import numpy as np
from plotly.subplots import make_subplots
from plotly.graph_objs import Contour, Scatter
import plotly.graph_objects as go

figure = make_subplots(rows=1, cols=3, shared_yaxes=True, column_widths=[0.4, 0.8, 1.2], horizontal_spacing=0.01)

t = np.linspace(0, 2*np.pi, 100)

trace_1 = Scatter(x = np.cos(t),
                  y = t,
                  mode = 'lines')

trace_2 = Contour(z=[[10, 12.5, 20],
                         [5.625, 8.125, 15.625],
                         [2.5, 5., 12.5],
                         [0.625, 3.125, 10.625],
                         [0, 2.5, 10]],
                  x=[-9, -7 , -5], # horizontal axis
                  y=[0, 1, 4, 5, 7], # vertical axis
                  contours_coloring = 'heatmap',
                  contours_showlabels = True,
                  colorbar = dict(x=0.47,  thickness=15)  # colorbar position
                  )

figure.add_trace(trace_1, row=1, col=1)
figure.add_trace(trace_2, row=1, col=2)

figure.update_layout(title_text = 'Title<br><span style="font-size:14px">Subtitle</span>',
                     xaxis1 = dict(title='x1', constrain='domain'),
                     xaxis2 = dict(title='x2', constrain='domain'),
                     yaxis = dict(title='y', constrain='domain', scaleanchor="x2", scaleratio=1))



figure.show(width=1080, height=720)

Have a nice day.

1 Like

Thanks, it does work !
Unfortunately, I was looking for a more generic answer, as my multiple figures (in fact, the Contours) are not all the same size.
Doesn’t Plotly offer such a functionality ?