This is going to be a somewhat long post, so please bear with me.
I’m used to be able to either use tight_layout or constrained_layout to produce plots that are nice and tight, but there seems to be a lack of support for both tight_layout and constrained_layout when it comes to ImageGrid plots. Below is an example where I plot a 5 x 2 grid with ImageGrid and use constrained_layout=True:
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid # Data to populate plots. im = np.arange(100).reshape((10, 10)) fig = plt.figure(figsize=(3, 6), constrained_layout=True) grid = ImageGrid(fig, 111, nrows_ncols=(5, 2), axes_pad=0.2, label_mode="L") for ax in grid: ax.imshow(im) fig.suptitle("Testing a suptitle on with ImageGrid") fig.savefig("imagegrid_suptitle.png")
This results in the following warning:
UserWarning: There are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keyword. The plot that is produced has a lot of dead-space on the top, sides, and bottom:
Compare this to the plot of a subplots figure that also uses constrained_layout:
import numpy as np import matplotlib.pyplot as plt # Data to populate plots. im = np.arange(100).reshape((10, 10)) fig, axs = plt.subplots(5, 2, figsize=(3, 6), sharex=True, sharey=True, constrained_layout=True) for ax in axs.flat: ax.imshow(im) fig.suptitle("Testing a suptitle with subplots") fig.savefig("subplots_suptitle.png")
You’ll have to take a look at the second plot here since I’m only allowed to upload one embedded image as a new user: https://imgur.com/a/fGS1WiT
In the subplots figure there’s a minimum of dead-space, and the suptitle is very close to the top of the two upmost subplots.
Using tight_layout instead gives bad results with both ImageGrid and subplots, and the following warning is produced by the ImageGrid plot:
UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. Check the third and fourth plots here: https://imgur.com/a/fGS1WiT
So, is there something I’m missing, or doesn’t constrained_layout support ImageGrid? If that’s the case how can I get rid of the dead-space?