首页 > 解决方案 > 在 jupyter notebook 中并排显示图像

问题描述

我想在我的 jupyter 笔记本中并排显示带有 matplotlib 的专辑中的一些图像。

我写了一个函数,但它不起作用。

import cv2
import numpy as np
import matplotlib.pyplot as plt

def show(path):
    for iter in list.get(path):
        img = cv2.imread(images)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        plt.axis('off')
        plt.imshow(img)
        plt.show()
        fig, ax = plt.subplots(nrows=2, ncols=2)

标签: pythonmatplotlib

解决方案


您只需要为每个图像使用 subplot 函数:

plt.subplot(2, 2, n) # n is the position of your subplot (1 to 4)
plt.imshow(img)

加载完所有子图后,只需调用:

plt.show()

下面我做了一个简单的例子,可能对你有帮助,你可以在我的github repo中找到这个文件的 Jupyter notebook :

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("a"+".jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.axis('off')

plt.subplot(2, 2, 1)
plt.imshow(img)

plt.subplot(2, 2, 2)
plt.imshow(img)

plt.subplot(2, 2, 3)
plt.imshow(img)

plt.subplot(2, 2, 4)
plt.imshow(img)

plt.show()

结果是:

侧面


推荐阅读