7

My subplot results subplot

My code

fig,ax = plt.subplots(rows,cols, figsize = [24,24])
plt.subplots_adjust(hspace=0, wspace=0)

for i in range(cols):
    step = 6
    ind = i*step
    ax[0,i].imshow(a[ind,:,:],cmap='gray')
    ax[0,i].axis('off')

    ax[1,i].imshow(b[ind,:,:],cmap='gray')
    ax[1,i].axis('off')

    ax[2,i].imshow(c[ind,:,:],cmap='gray')
    ax[2,i].axis('off')

However, it seems like plt.subplots_adjust(hspace=0, wspace=0) not working at all. I notice that it forces the figure to have equal x and y size, could you help me correct this?

2
  • gridspec might help Commented May 10, 2017 at 13:11
  • 1
    or plt.tight_layout() Commented May 10, 2017 at 13:13

4 Answers 4

8

You may either shrink the figure size in vertical direction, e.g.

fig,ax = plt.subplots(rows,cols, figsize = [24,12])

or you may keep the square figure size but put more margin around the subplots

plt.subplots_adjust(bottom=0.3, top=0.7, hspace=0)
Sign up to request clarification or add additional context in comments.

Comments

4

The ratio in figsize should be same as the ratio of rows and cols in plt.subplot(rows, cols, figsize). For example, if rows is 2 and cols is 4, then the ratio is 1/2, thus figsize being (15, 7.5)(same ratio) would be good.

Following code is an example.

fig, ax = plt.subplots(2, 4, figsize=(15, 7.5))
for i in range(2):
    for j in range(4):
        img = cv2.cvtColor(cv2.imread(os.path.join('../input/deepfake486326facescleaned', train_df.loc[i*2+j, 'name_path'])), cv2.COLOR_BGR2RGB)
        ax[i][j].imshow(img)
        ax[i][j].set_title(train_df.loc[i*2+j, 'label'])

enter image description here

Comments

2

You have used figsize=[24,24] to make it a square image. subplots_adjust then makes each ax[i,j] to stick to the adjacent one. But ax.imshow() does not fill each ax. In your sample above, if the vertical size of the image were larger, it would have filled the area alloted to that axes, but by distorting the image. If you want to try that out, use the command ax[0,i].imshow(a[ind,:,:],cmap='gray', aspect='auto'): the aspect='auto' part will stretch your images to fill the axes. If you want the image aspect ratio to not be distorted, then you have to edit your figsize accordingly.

Comments

1

You can use the command fig.tight_layout(h_pad=-40.0) to reduce the horizontal gap between the two lines of subplot.

Adjust the number in the parentheses to adjust the gap. Negative number reduces the gap and a positive value increases the gap.

Code to print 4 images with names A, B, C, D:

fig = plt.figure(figsize = (15, 15)) 

fig.add_subplot(2,2,1)
plt.title("A")
plt.imshow(A,cmap='gray')
plt.axis('off')

fig.add_subplot(2,2,2)
plt.title("B")
plt.imshow(B,cmap='gray')
plt.axis('off')

fig.add_subplot(2,2,3)
plt.title("C")
plt.imshow(C,cmap='gray')
plt.axis('off')

fig.add_subplot(2,2,4)
plt.title("D")
plt.imshow(D,cmap='gray')
plt.axis('off') 

fig.tight_layout(h_pad=-40.0)#reduces the horizontal gap

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.