I’m trying to build a figure with 9 image subplots on a 3×3 grid, all sharing X or Y axes, and with no space between adjacent subplots.
The following code adds the required axis and collapses the space between them. So far, so good:
fig, axes = plt.subplots(ncols=3, nrows=3, sharex=True, sharey=True)
fig.subplots_adjust(hspace=0, wspace=0)
However, plotting images inside these plots breaks everything, because imshow
changes the aspect ratio of the subplots:
img = np.arange(100).reshape(10, 10)
for ax in axes.flat:
ax.imshow(img)
(If the image is too wide, you get spaces between rows instead of columns as shown on this figure.)
Question: how to make a figure containing image subplots with no space between adjacent axes, regardless of the aspect ratio of the images?
What can’t be an answer:
- Cropping the images
- Changing their aspect ratio (mandatory XKCD).