简体   繁体   English

如何在子图中 plot 图像

[英]How to plot images in subplots

Suppose I have 3 directories of.jpg files: dataset 1, dataset 2, dataset 3.假设我有 3 个 .jpg 文件目录:数据集 1、数据集 2、数据集 3。

I would like to make a 5 by 3 subplots using matplotlib.我想使用 matplotlib 制作一个 5 x 3 的子图。 For each row, the subplot shows the data from dataset 1, dataset 2 and dataset 3 in order.对于每一行,子图按顺序显示来自数据集 1、数据集 2 和数据集 3 的数据。 The expected format is like this:预期的格式是这样的:

plot1, plot2, plot3,情节1,情节2,情节3,

plot4.......情节4.......

plot13, plot14, plot15.情节 13,情节 14,情节 15。

How should I do that?我该怎么做?

something like this:像这样的东西:

plt.figure(figsize=(10, 10)) 
for data1, data2, data3 in dataset1, dataset2, dataset3"
....
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)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM