2

I have 10 image ids. From each ID, I can have raw image, ground truth, preprocessing, postprocessing path. I will read the image from each path and draw in a figure with sub-figures: columns indicates the fourth type of images: raw, gt, pre, post, while the rows indicate for image id from 1 to 10.

Currently, I use gridspec to locate the sub figures and the axis from 1 to 40 for each image. I use a loop to read images on the list, then use conditions for place images in each axis. However, it looks very long code that I think Python and matplotlib can have a better way. Could you suggest to me the way? This is my current implementation

if __name__ == "__main__":

    fig = plt.figure(figsize=(50, 50))
    fig.patch.set_facecolor('white')
    gs1 = gridspec.GridSpec(4, 10)
    gs1.update(wspace=0.01, hspace=0.01)  # set the spacing between axes.

    ax1 = plt.subplot(gs1[0])
    ..
    ax40 = plt.subplot(gs1[39])

    ax1.axis('off')
    ...
    ax40.axis('off')

    ax37.text(0.5, -0.1, "(a)", size=20, ha="center",
              transform=ax13.transAxes)
    ax38.text(0.5, -0.1, "(b)", size=20, ha="center",
              transform=ax14.transAxes)
    ax39.text(0.5, -0.1, "(c)", size=20, ha="center",
              transform=ax15.transAxes)
    ax40.text(0.5, -0.1, "(d)", size=20, ha="center",
              transform=ax16.transAxes)

    image_id_list=['2011_1', '2011_2', '2012_1', '2012_1'...] #10 images id

    for i in range (len(image_id_list)):
        image_id=image_id_list[i] 
        raw_image_path='./Images/+ image_id +'jpg' 
        gt_image_path='./GT/+ image_id +'jpg'
        pre_image_path='./Pre/+ image_id +'jpg' 
        post_image_path='./Post/+ image_id +'jpg'        
        raw_image=Image.open(raw_image_path)       
        gt_image=Image.open(gt_image_path)
        pre_image=Image.open(pre_image_path) 
        post_image=Image.open(post_image_path)    
        if (i==0):
            ax1.imshow(raw_image)
            ax2.imshow(gt_image)
            ax3.imshow(pre_image)
            ax4.imshow(post_image)
        if (i==1):
            ax5.imshow(raw_image)
            ax6.imshow(gt_image)
            ax7.imshow(pre_image)
            ax8.imshow(post_image)
        if (i==2):
            ax9.imshow(raw_image)
            ax10.imshow(gt_image)
            ax11.imshow(pre_image)
            ax12.imshow(post_image)
        if (i==3):
            ax13.imshow(raw_image)
            ax14.imshow(gt_image)
            ax15.imshow(pre_image)
            ax16.imshow(post_image)
        ...

    plt.show()
    fig.savefig('./result.png',bbox_inches='tight')  # save the figure to file
    plt.close(fig)  # close the figure
Jame
  • 3,746
  • 6
  • 52
  • 101

2 Answers2

14

How about this:

import os
import matplotlib.pyplot as plt
import PIL
%matplotlib inline

rows = 2
os.chdir('/home/brian/Desktop/cats/')
files = os.listdir('/home/brian/Desktop/cats/')

for num, x in enumerate(files):
    img = PIL.Image.open(x)
    plt.subplot(rows,6,num+1)
    plt.title(x.split('.')[0])
    plt.axis('off')
    plt.imshow(img)

enter image description here

briancaffey
  • 2,339
  • 6
  • 34
  • 62
4

Opening 40 Axes 'by hand' is quite cumbersome. Especially if all Axes are the same size, it is better to use the plt.subplots() function, which returns a numpy array of axes that can be easily indexed or looped through. Look if this code works for you (it's hard to test, as we don't have your input images):

from matplotlib import pyplot as plt
import numpy as np

fig,axes = plt.subplots(nrows = 4, ncols = 10, figsize=(50,50))

for ax in axes.flatten():
    ax.axis('off')

##edit this line to include your own image ids
image_id_list=['{}_{}'.format(i,j) for i in range(2011,2016) for j in range(1,3)]

for i,image_id in enumerate(image_id_list):
    raw_image_path='./Images/'+ image_id +'jpg'
    raw_image = Image.open(raw_image_path)
    axes[0,i].imshow(raw_image)

    gt_image_path='./Images/'+ image_id +'jpg'
    gt_image = Image.open(gt_image_path)
    axes[0,i].imshow(gt_image)

    pre_image_path='./Images/'+ image_id +'jpg'
    pre_image = Image.open(pre_image_path)
    axes[0,i].imshow(pre_image)

    post_image_path='./Images/'+ image_id +'jpg'
    post_image = Image.open(post_image_path)
    axes[0,i].imshow(post_image)

plt.show()
Thomas Kühn
  • 9,412
  • 3
  • 47
  • 63