How to plot neat ImageGrid plots?

Viewed 307

When I try to plot figures with limited amounts of dead-space on the top, sides, and bottom I use either tight_layout or constrained_layout. However, it seems like ImageGrid doesn't support this. Take a look at the following example:

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=(4, 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")

I get the following warning:

UserWarning: There are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keyword fig.savefig("imagegrid_suptitle.png")

And this is the resulting plot: ImageGrid with constrained_layout=True

As can be seen, there's a lot of dead-space at the top, sides, and bottom, and the suptitle is located far above the top two grids. Is this a bug in constrained_layout, or is it simply not possible to support its use with ImageGrid?

Please note that I have tried to use tight_layout, but that produces a warning about the lack of support for ImageGrid:

UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect. fig.tight_layout()

It also results in the suptitle overlapping with the two top grids.

0 Answers
Related