0

I am trying to align my subplots. I have tried several combinations but these are the closest that I get. I simply need the figures to be close to each other and have the title on the left end of the row. What am I missing from this supposedly easy task?

This variant adds uneven spacing.

# Input
fig, axs = plt.subplots(1,10,figsize=(15,3), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
fig.suptitle('Input', x=0.1, y=0.6)
for i in range(1):
    for j in range(10):
        im = x_true_np[1,i*20+j]
        axs[i][j].imshow(im, cmap="gray")
        axs[i][j].axis('off')
# Ground Truth
fig, axs = plt.subplots(1,10,figsize=(15,3), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
fig.suptitle('Ground Truth', x=0.1, y=0.6)
for i in range(1):
    for j in range(0,10):
        im = x_true_np[1,i*20+j+10]
        axs[i][j].imshow(im, cmap="gray")
        axs[i][j].axis('off')
# Predictions
fig, axs = plt.subplots(1,10,figsize=(15,3), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
fig.suptitle('Predictions', x=0.1, y=0.6)
for i in range(1):
    for j in range(0,10):
        im = x_pred_np[1,i*20+j]
        axs[i][j].imshow(im, cmap="gray")
        axs[i][j].axis('off')

enter image description here

This variant ignores the hspace parameter and doesn't even display the titles.

fig, axs = plt.subplots(3,10,figsize=(15,10), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
axs[0][0].set_ylabel("Input", fontsize=20)
for j in range(10):
    im = x_true_np[1,j]
    axs[0][j].imshow(im, cmap="gray")
    axs[0][j].axis('off')
axs[1][0].set_ylabel("Ground Truth", fontsize=20)
for j in range(10):
    im = x_true_np[1,10+j]
    axs[1][j].imshow(im, cmap="gray")
    axs[1][j].axis('off')
axs[2][0].set_ylabel("Ground Truth", fontsize=20)
for j in range(10):
    im = x_pred_np[1,j]
    axs[2][j].imshow(im, cmap="gray")
    axs[2][j].axis('off')    
fig.tight_layout() 

sub

halfer
  • 19,824
  • 17
  • 99
  • 186
Kong
  • 2,202
  • 8
  • 28
  • 56
  • 1
    The axes have a set aspect ratio, so you need to adjust the figure size to reduce the white space. To get the label, you could add an 11th subplot on the left side of each row. You might also be interested in the axes_grid toolbox. – Jody Klymak Oct 23 '19 at 05:22

1 Answers1

0

I ended up using the ImageGrid toolbox. So much easier.

from mpl_toolkits.axes_grid1 import ImageGrid

idxs = [6,7,8,9]

for idx in idxs:
    fig  = plt.figure(idx, (15, 10))
    grid = ImageGrid(fig, 111, nrows_ncols=(3, 10), axes_pad=0.1)
    for i in range(3):
        grid[0].set_ylabel("Input")
        grid[0].set_ylabel("Ground Truth")
        grid[0].set_ylabel("Prediction")
        for j in range(10):
            grid[j].imshow(x_true_np[idx,j], cmap="gray")
            grid[j+10].imshow(x_true_np[idx,j+10], cmap="gray")
            grid[j+20].imshow(x_pred_np[idx,j], cmap="gray")

enter image description here

Kong
  • 2,202
  • 8
  • 28
  • 56