Too much white space around subplots in 3d projection

I cannot figure out how to reduce the margins around the subplots in the case of 3d projection. I tried subplots_adjust, but it does not seem to be working. Moreover, it came to my attention that this happens for the latest versions of Matplotlib, yet it does not happen in the earlier versions, such as 3.1.2. I tried to use fig.subplots_adjust(wspace=0, hspace=0) but I did not succeed. Any ideas? Thanks.

fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(19.2, 10.8), 
                         subplot_kw=dict(projection='3d'), constrained_layout=True)
fig.subplots_adjust(hspace=0,wspace=0)
fig.tight_layout(rect=[0, 0.0, 1, 0.94])
fig.savefig('trial.png', format='png', dpi=220)
fig.show()

Try this:

fig, ax = plt.subplots(subplot_kw=dict(projection='3d'),
                       gridspec_kw=dict(top=1, left=0, right=1, bottom=0))

I have tried, but no change (also without constrained_layout). The horizontal distance between subplots is quite large.

import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(19.2, 10.8), 
                         subplot_kw=dict(projection='3d'), constrained_layout=True, gridspec_kw=dict(top=1, left=0, right=1, bottom=0))

You are using incompatible layout methods, and hoping one will stick. tight_layout overwrites subplots_adjust, so don’t combine; similarly with setting the top.left etc of the gridspec - they will get overwritten by the automatic layout engines

tight_layout and constrained_layout are incompatible so don’t mix those.

Finally you will note that you do not have a lot of white space in the y direction, and that your figure is substantially wider than it is tall, so there will be white space in the horizontal direction. By default, Matplotlib spaces those equally across the figure, even using constrained or tight layout The most robust fix is to make the figure a better aspect ratio.

You can also try plt.subplots(..., layout='compressed') with 3.6 or greater Matplotlib. Though on my machine that cuts off the y tick labels, which don’t appear to be correctly included in the axes bounding box for 3D axes.

1 Like