【问题标题】:How to plot images in subplots如何在子图中绘制图像
【发布时间】:2021-05-19 01:25:17
【问题描述】:

假设我有 3 个 .jpg 文件目录:数据集 1、数据集 2、数据集 3。

我想使用 matplotlib 制作一个 5 x 3 的子图。对于每一行,子图按顺序显示来自数据集 1、数据集 2 和数据集 3 的数据。预期的格式是这样的:

情节1,情节2,情节3,

情节4.......

情节13,情节14,情节15。

我该怎么做?

类似这样的:

plt.figure(figsize=(10, 10)) 
for data1, data2, data3 in dataset1, dataset2, dataset3"
....

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:
    import matplotlib.pyplot as plt
    from pathlib import Path
    
    # create a list of directories
    dirs = ['../Pictures/dataset1', '../Pictures/dataset2', '../Pictures/dataset3']
    
    # extract the image paths into a list
    files = [f for dir_ in dirs for f in list(Path(dir_).glob('*.jpg'))]
    
    # create the figure
    fig, axs = plt.subplots(nrows=5, ncols=3, figsize=(10, 10))
    
    # flatten the axis into a 1-d array to make it easier to access each axes
    axs = axs.flatten()
    
    # iterate through and enumerate the files, use i to index the axes
    for i, file in enumerate(files):
        
        # read the image in
        pic = plt.imread(file)
    
        # add the image to the axes
        axs[i].imshow(pic)
    
        # add an axes title; .stem is a pathlib method to get the filename
        axs[i].set(title=file.stem)
    
    # add a figure title
    fig.suptitle('Images from https://www.heroforge.com/', fontsize=18)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      相关资源
      最近更新 更多