Open In App

How to Display Multiple Images in One Figure Correctly in Matplotlib?

Last Updated : 25 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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. Then will display the image using imshow() method.

Syntax: add_subplot(rows, columns, i)

 Here rows and columns are the total number of rows and columns in the figure and i is the position at which new subplot must be placed.

Steps:

  • Import required libraries
  • Create a figure
  • Set values of rows and column variables
  • Read images
  • Add subplot and display image one by one

Below is the implementation :

In Matplotlib, we can achieve this by creating a grid of subplots within a single figure, and then placing each image in one of the grid positions. The process involves using functions like figure(), add_subplot(), and imshow() to handle the figure creation and image display.

However, when using OpenCV (cv2) to read images, it’s important to remember that OpenCV loads images in the BGR (Blue, Green, Red) format, while Matplotlib expects images in the RGB (Red, Green, Blue) format. Thus, before displaying the images with Matplotlib, we need to convert them from BGR to RGB.

This method provides an easy way to arrange images in rows and columns and makes it simple to display and compare multiple images in a single visual output. Let’s break down the steps involved in this process:

Python
import cv2 
from matplotlib import pyplot as plt  

fig = plt.figure(figsize=(10, 7))

# Read the images using OpenCV (OpenCV loads images in BGR format)
image1 = cv2.imread('/content/Image1.jpg')
image2 = cv2.imread('/content/Image2.jpg')
image3 = cv2.imread('/content/Image3.jpg')
image4 = cv2.imread('/content/Image4.jpg')

# Convert the images from BGR to RGB format so Matplotlib can display them correctly
image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2RGB)
image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2RGB)

# Add the first image to the figure (top-left position)
plt.subplot(2, 2, 1)  # 2 rows, 2 columns, first position
plt.imshow(image1)  
plt.axis('off')  # Hide the axis labels
plt.title("Image 1") 

# Add the second image to the figure (top-right position)
plt.subplot(2, 2, 2)  # 2 rows, 2 columns, second position
plt.imshow(image2)  
plt.axis('off')  # Hide the axis labels
plt.title("Image 2") 

# Add the third image to the figure (bottom-left position)
plt.subplot(2, 2, 3)  # 2 rows, 2 columns, third position
plt.imshow(image3) 
plt.axis('off')  # Hide the axis labels
plt.title("Image 3")  
# Add the fourth image to the figure (bottom-right position)
plt.subplot(2, 2, 4)  # 2 rows, 2 columns, fourth position
plt.imshow(image4)  
plt.axis('off')  # Hide the axis labels
plt.title("Image 4")  

plt.show()

Output:

Multiple-Images-in-One-Figure-Correctly-in-Matplotlib

Multiple Images in One Figure Correctly in Matplotlib




Next Article
Article Tags :
Practice Tags :

Similar Reads