matplotlib 在子图中显示多个图像

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

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

  1. for file in images:
  2. process(file)
  3. def process(filename):
  4. image = mpimg.imread(filename)
  5. <something gets done here>
  6. plt.imshow(image)

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

pw9qyyiw

pw9qyyiw1#

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

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

kxxlusnw2#

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

  1. import matplotlib.pyplot as plt
  2. import matplotlib.image as mpimg
  3. def process(filename: str=None) -> None:
  4. """
  5. View multiple images stored in files, stacking vertically
  6. Arguments:
  7. filename: str - path to filename containing image
  8. """
  9. image = mpimg.imread(filename)
  10. # <something gets done here>
  11. plt.figure()
  12. plt.imshow(image)
  13. for file in images:
  14. process(file)

这将垂直堆叠图像

展开查看全部
x33g5p2x

x33g5p2x3#

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

  1. from typing import Union,List
  2. import numpy
  3. import cv2
  4. import os
  5. def load_image(image: Union[str, numpy.ndarray]) -> numpy.ndarray:
  6. # Image provided ad string, loading from file ..
  7. if isinstance(image, str):
  8. # Checking if the file exist
  9. if not os.path.isfile(image):
  10. print("File {} does not exist!".format(imageA))
  11. return None
  12. # Reading image as numpy matrix in gray scale (image, color_param)
  13. return cv2.imread(image, 0)
  14. # Image alredy loaded
  15. elif isinstance(image, numpy.ndarray):
  16. return image
  17. # Format not recognized
  18. else:
  19. print("Unrecognized format: {}".format(type(image)))
  20. print("Unrecognized format: {}".format(image))
  21. return None

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

  1. import matplotlib.pyplot as plt
  2. def show_images(images: List[numpy.ndarray]) -> None:
  3. n: int = len(images)
  4. f = plt.figure()
  5. for i in range(n):
  6. # Debug, plot figure
  7. f.add_subplot(1, n, i + 1)
  8. plt.imshow(images[i])
  9. plt.show(block=True)

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

展开查看全部
p8ekf7hl

p8ekf7hl4#

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

  1. for file in images:
  2. process(file)
  3. def process(filename):
  4. image = mpimg.imread(filename)
  5. # <something gets done here>
  6. plt.imshow(image)
  7. plt.show()
llew8vvj

llew8vvj5#

根据Aadhar Bhatt的回答:

  1. from matplotlib.image import imread
  2. import matplotlib.pyplot as plt
  3. v_slice = [] #create an empty list called v_slice
  4. for i in range(0,4):
  5. image = imread("test.png") #Here I load the same image 4 times-replace this with code that generates images
  6. v_slice.append(image)
  7. #Aadhar Bhatt's answer
  8. plt.figure()
  9. #subplot(r,c) provide the no. of rows and columns
  10. f, axarr = plt.subplots(4,1)
  11. # use the created array to output your multiple images. In this case I have stacked 4 images vertically
  12. axarr[0].imshow(v_slice[0])
  13. axarr[1].imshow(v_slice[1])
  14. axarr[2].imshow(v_slice[2])
  15. axarr[3].imshow(v_slice[3])
展开查看全部

相关问题