matplotlib 在子图中显示多个图像

tcomlyy6  于 2023-10-24  发布在  其他
关注(0)|答案(5)|浏览(129)

如何使用matlib函数plt.imshow(image)显示多个图像?
例如,我的代码如下:

for file in images:
    process(file)

def process(filename):
    image = mpimg.imread(filename)
    <something gets done here>
    plt.imshow(image)

我的结果表明,只有最后处理的图像有效地显示了其他图像

pw9qyyiw

pw9qyyiw1#

要显示多个图像,请使用subplot()

plt.figure()

#subplot(r,c) provide the no. of rows and columns
f, axarr = plt.subplots(4,1) 

# use the created array to output your multiple images. In this case I have stacked 4 images vertically
axarr[0].imshow(v_slice[0])
axarr[1].imshow(v_slice[1])
axarr[2].imshow(v_slice[2])
axarr[3].imshow(v_slice[3])
kxxlusnw

kxxlusnw2#

您可以使用以下方法设置框架以显示多个图像:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

def process(filename: str=None) -> None:
    """
    View multiple images stored in files, stacking vertically

    Arguments:
        filename: str - path to filename containing image
    """
    image = mpimg.imread(filename)
    # <something gets done here>
    plt.figure()
    plt.imshow(image)

for file in images:
    process(file)

这将垂直堆叠图像

x33g5p2x

x33g5p2x3#

在第一个示例中,将图像从文件加载到numpy矩阵中

from typing import Union,List
import numpy
import cv2
import os
def load_image(image: Union[str, numpy.ndarray]) -> numpy.ndarray:
    # Image provided ad string, loading from file ..
    if isinstance(image, str):
        # Checking if the file exist
        if not os.path.isfile(image):
            print("File {} does not exist!".format(imageA))
            return None
        # Reading image as numpy matrix in gray scale (image, color_param)
        return cv2.imread(image, 0)

    # Image alredy loaded
    elif isinstance(image, numpy.ndarray):
        return image

    # Format not recognized
    else:
        print("Unrecognized format: {}".format(type(image)))
        print("Unrecognized format: {}".format(image))
    return None

然后,您可以使用以下方法绘制多个图像:

import matplotlib.pyplot as plt
def show_images(images: List[numpy.ndarray]) -> None:
    n: int = len(images)
    f = plt.figure()
    for i in range(n):
        # Debug, plot figure
        f.add_subplot(1, n, i + 1)
        plt.imshow(images[i])

    plt.show(block=True)

show_images方法输入一个图像列表,您可以使用load_image方法迭代读取这些图像。

p8ekf7hl

p8ekf7hl4#

在for循环中,在plt.imshow(image)之后使用plt.show()对我来说很有效。

for file in images:
    process(file)
    
def process(filename):
    image = mpimg.imread(filename)
    # <something gets done here>
    plt.imshow(image)
    plt.show()
llew8vvj

llew8vvj5#

根据Aadhar Bhatt的回答:

from matplotlib.image import imread
import matplotlib.pyplot as plt

v_slice = [] #create an empty list called v_slice
for i in range(0,4):
    image = imread("test.png") #Here I load the same image 4 times-replace this  with code that generates images
    v_slice.append(image)

#Aadhar Bhatt's answer
plt.figure()
#subplot(r,c) provide the no. of rows and columns
f, axarr = plt.subplots(4,1) 
# use the created array to output your multiple images. In this case I have stacked 4 images vertically
axarr[0].imshow(v_slice[0])
axarr[1].imshow(v_slice[1])
axarr[2].imshow(v_slice[2])
axarr[3].imshow(v_slice[3])

相关问题