0

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)

3×3 grid of empty axes with no space between adjacent axes

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)

3×3 grid of subplots containing square images, with a space visible between adjacent image columns

(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:

Arcturus B
  • 5,181
  • 4
  • 31
  • 51
  • 1
    There are at least those three questions on the same topic: 1. https://stackoverflow.com/questions/42675864/how-to-remove-gaps-between-images-in-matplotlib 2. https://stackoverflow.com/questions/42475508/how-to-combine-gridspec-with-plt-subplots-to-eliminate-space-between-rows-of-s 3. https://stackoverflow.com/questions/41071947/how-to-remove-the-space-between-subplots-in-matplotlib-pyplot Maybe you can comment in how far they would not help here. – ImportanceOfBeingErnest Oct 05 '18 at 11:55
  • Thanks @ImportanceOfBeingErnest, I’m looking into this. – Arcturus B Oct 05 '18 at 11:58

2 Answers2

1

Partial answer for 2×2 grids

It is possible to use ax.set_anchor to align each image within its assigned space:

fig, axes = plt.subplots(ncols=2, nrows=2, sharex=True, sharey=True)
fig.subplots_adjust(hspace=0, wspace=0)
axes[0, 0].set_anchor('SE')
axes[1, 0].set_anchor('NE')
axes[0, 1].set_anchor('SW')
axes[1, 1].set_anchor('NW')
img = np.arange(100).reshape(10, 10)
for ax in axes.flat:
    ax.imshow(img)

2×2 image subplots grid with no space between the images

This works also for the wider aspect ratio, but it breaks down for grids that are wider or taller than 2 axes.

Arcturus B
  • 5,181
  • 4
  • 31
  • 51
1

You can specify the figure size so that it will have a different aspect ratio; in this case a square:

import numpy as np
from matplotlib import pyplot as plt    

fig, axes = plt.subplots(ncols=3, nrows=3, sharex=True, sharey=True, figsize=(5,5))
fig.subplots_adjust(hspace=0, wspace=0)

img = np.arange(100).reshape(10, 10)
for ax in axes.flat:
    ax.imshow(img)
plt.show()

It's not the most flexible solution, but it's a fairly straightforward solution.

enter image description here

9769953
  • 10,344
  • 3
  • 26
  • 37
  • Thanks for the suggestion! However there is still a tiny gap between the axes. On your figure, the vertical lines are thicker than the horizontal ones; see also this [zoom on the center of a 2×2 grid](https://i.stack.imgur.com/s8YAL.png). While it’s a great start, it’s not good enough for publication. – Arcturus B Oct 05 '18 at 09:16