Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using plt.imshow() to display multiple images

How do I use the matlib function plt.imshow(image) to display multiple images?

For example my code is as follows:

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

My results show that only the last processed image is shown effectively overwriting the other images

like image 605
zooter Avatar asked Dec 18 '16 17:12

zooter


People also ask

How do I show multiple images on PLT Imshow?

The easiest way to display multiple images in one figure is use figure(), add_subplot(), and imshow() methods of Matplotlib. The approach which is used to follow is first initiating fig object by calling fig=plt. figure() and then add an axes object to the fig by calling add_subplot() method.

How do I use multiple images on Imshow?

imshow always displays an image in the current figure. If you display two images in succession, the second image replaces the first image. To view multiple figures with imshow , use the figure command to explicitly create a new empty figure before calling imshow for the next image.


2 Answers

You can set up a framework to show multiple images using the following:

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) 

This will stack the images vertically

like image 129
datawrestler Avatar answered Sep 30 '22 14:09

datawrestler


To display the multiple images use 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]) 
like image 39
Aadhar Bhatt Avatar answered Sep 30 '22 13:09

Aadhar Bhatt