Setting constrained_layout_pads in plt.subplots

I can set constrained_layout_pads with

fig, ax = plt.subplots(layout='constrained')
fig.set_constrained_layout_pads(w_pad=0.5, h_pad=0.5)

I can also do it in one call: plt.subplots(constrained_layout={'w_pad': 0.5, 'h_pad': 0.5})

So far so good.

According to matplotlib.figure — Matplotlib 3.8.2 documentation I should be able to pass figure kwargs in plt.subplots():

fig, ax = plt.subplots(layout='constrained',
                       constrained_layout_pads={'w_pad': 0.5, 'h_pad': 0.5})`

which results, however, in a

TypeError: set_constrained_layout_pads() takes 1 positional argument but 2 were given

The documentation mentions a single value (float), although the parameter name is in plural:

constrained_layout_pads float, default: rcParams["figure.constrained_layout.w_pad"] (default: 0.04167)

but plt.subplots(constrained_layout_pads=0.5) results in the same error.

So my question is how to use constrained_layout_pads in the plt.subplots call.

@StefRe I’d open an issue about this. a) it appears somehow the constrained_layout_pads has leaked into the kwargs of Figure because of `set_constrained_layout_pads method. I don’t quite understand why that has happened, but that is meant to be a method that is passed multiple kwargs.

see [Bug]: constrained_layout_pads as kwargs of Figure · Issue #22029 · matplotlib/matplotlib · GitHub

1 Like