Reduce the gap between rows when using matplotlib subplot?

后端 未结 3 1746
-上瘾入骨i
-上瘾入骨i 2021-01-21 15:11

My subplot results

My code

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

for i in range(cols):
    step          


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 15:45

    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'])
    

提交回复
热议问题