【问题标题】:How to plot a list of image in loop using matplotlib? [duplicate]如何使用matplotlib循环绘制图像列表? [复制]
【发布时间】:2018-07-04 06:01:16
【问题描述】:

我有 10 个图像 ID。从每个 ID,我可以获得原始图像、基本事实、预处理、后处理路径。我会从每条路径中读取图像,并绘制一个带有子图的图:列表示第四类图像:raw、gt、pre、post,而行表示从 1 到 10 的图像 id。

目前,我使用gridspec 来定位每个图像的子图形和从 1 到 40 的轴。我使用循环读取列表中的图像,然后使用条件在每个轴中放置图像。但是,看起来很长的代码,我认为 Python 和 matplotlib 可以有更好的方法。你能给我建议一下方法吗?这是我目前的实现

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

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

    这个怎么样:

    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)
    

    【讨论】:

    • 看起来不错,但我还必须考虑列信息。请阅读我的问题
    • 好的,我去看看@Jame
    【解决方案2】:

    “手动”打开 40 Axes 相当麻烦。特别是如果所有Axes 的大小相同,最好使用plt.subplots() 函数,它返回一个numpy 轴数组,可以轻松索引或循环遍历。看看这段代码是否适合你(很难测试,因为我们没有你的输入图像):

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

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2013-04-24
      相关资源
      最近更新 更多