Search code examples
pythonmatplotlibsubplotimshow

Display multiple images in subplots


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


Solution

  • 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