Displaying different images with actual size in a Matplotlib subplot

To display different images with actual size in a Matplotlib subplot, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Read two images using imread() method (im1 and im2)
  • Create a figure and a set of subplots.
  • Turn off axes for both the subplots.
  • Use imshow() method to display im1 and im2 data.
  • To display the figure, use show() method.

Example

Here's how to display multiple images in subplots while maintaining their actual sizes ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create sample images for demonstration
im1 = np.random.randint(0, 255, (200, 300, 3), dtype=np.uint8)  # RGB image
im2 = np.random.randint(0, 255, (150, 200, 3), dtype=np.uint8)  # Different size RGB image

fig, ax = plt.subplots(nrows=1, ncols=2)

ax[0].axis('off')
ax[0].imshow(im1)
ax[0].set_title('Image 1')

ax[1].axis('off')
ax[1].imshow(im2)
ax[1].set_title('Image 2')

plt.tight_layout()
plt.show()

Working with Different Image Formats

You can also display grayscale images or use different colormaps ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True

# Create grayscale and color images
gray_img = np.random.randint(0, 255, (200, 200), dtype=np.uint8)  # Grayscale
color_img = np.random.randint(0, 255, (200, 200, 3), dtype=np.uint8)  # Color

fig, ax = plt.subplots(nrows=1, ncols=2)

# Display grayscale image
ax[0].axis('off')
ax[0].imshow(gray_img, cmap='gray')
ax[0].set_title('Grayscale Image')

# Display color image
ax[1].axis('off')
ax[1].imshow(color_img)
ax[1].set_title('Color Image')

plt.tight_layout()
plt.show()

Key Points

  • axis('off') removes the axes and ticks for cleaner image display
  • imshow() automatically scales images to fit the subplot
  • tight_layout() adjusts spacing between subplots
  • Use cmap='gray' for grayscale images
  • Different sized images will be scaled to fit their respective subplots

Conclusion

Use subplots() with imshow() to display multiple images side by side. Turn off axes with axis('off') for cleaner presentation and use tight_layout() for proper spacing.

Updated on: 2026-03-25T21:37:52+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements