matplotlib:多个子图并排放置

引言

  • 论文中有时会用到介绍数据集,每类图像并排放置的显示方式,所以可以自己定制这种类型很有必要(如下图样式)

相关代码

# -*- coding:utf-8 -*-
from pathlib import Path
import random

from matplotlib import pyplot as plt
from PIL import Image

# 读取图像到列表中
image_path = Path(r'make_figure\fmd')
all_selected_images = []
selected_images = []

for one_category in image_path.iterdir():
    if one_category.is_dir():
        selected_images = list(one_category.iterdir())
        random.shuffle(selected_images)
        selected_images = selected_images[:10]
    all_selected_images.extend(selected_images)

# 绘制多个子图
plt.figure()
for i in range(1, 101):
    im = Image.open(str(all_selected_images[i-1]))
    im = im.resize((512, 384))
    ax = plt.subplot(10, 10, i)
    plt.imshow(im)

    # 去除坐标轴
    plt.xticks([])
    plt.yticks([])

    # 去除黑框
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['left'].set_visible(False)

# 设置各个子图间间距
plt.subplots_adjust(left=0.10, top=0.88, right=0.65, bottom=0.08, wspace=0.02, hspace=0.02)
plt.show()
posted @ 2020-02-24 17:36  Danno  阅读(4644)  评论(0编辑  收藏  举报