Display Images in Matplotlib Subplots with Custom Sizes

I’ve often needed to display multiple images side by side. Whether I was preparing a report for a client or analyzing satellite data for a project, one tool always came to my rescue: Matplotlib.

Matplotlib is one of the most powerful Python libraries for data visualization. What many people don’t realize is that it is not limited to line charts and bar graphs. You can also use it to display images in subplots with custom sizes.

I struggled with arranging images neatly in subplots. Sometimes the images looked stretched, and other times they overlapped. Over the years, I’ve learned some simple and effective methods to handle this and how to display multiple images in Matplotlib subplots and adjust their sizes to match your needs.

Display Images in Matplotlib Subplots (Python Methods)

When working with Python, displaying images in subplots is one of the most common tasks for data visualization. Let me show you two practical ways to do this.

Method 1 – Display Images Using imshow() in Subplots

The first method I usually use is the imshow() function. This is a built-in Matplotlib function that makes it simple to display images.

Here’s how you can do it in Python:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load sample images
img1 = mpimg.imread('usa_flag.png')   # Example: US flag image
img2 = mpimg.imread('newyork.jpg')    # Example: New York cityscape
img3 = mpimg.imread('grandcanyon.jpg') # Example: Grand Canyon landscape

# Create subplots
fig, axes = plt.subplots(1, 3, figsize=(12, 4))

# Display images in subplots
axes[0].imshow(img1)
axes[0].set_title("USA Flag")
axes[0].axis('off')

axes[1].imshow(img2)
axes[1].set_title("New York")
axes[1].axis('off')

axes[2].imshow(img3)
axes[2].set_title("Grand Canyon")
axes[2].axis('off')

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Images in Matplotlib Subplots with Custom Sizes

This code creates three subplots in a single row and displays the images clearly. I prefer this method when I need a quick way to display images without worrying too much about resizing. It works perfectly for quick reports and presentations.

Method 2 – Display Images with add_subplot()

Another method I’ve often used is add_subplot(). This gives me more control over how the subplots are arranged.

Here’s an example in Python:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load images
img1 = mpimg.imread('usa_flag.png')
img2 = mpimg.imread('newyork.jpg')

# Create a figure
fig = plt.figure(figsize=(8, 6))

# Add subplots manually
ax1 = fig.add_subplot(1, 2, 1)
ax1.imshow(img1)
ax1.set_title("USA Flag")
ax1.axis('off')

ax2 = fig.add_subplot(1, 2, 2)
ax2.imshow(img2)
ax2.set_title("New York")
ax2.axis('off')

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Matplotlib Subplots Images with Custom Sizes

I use this approach when I want to add subplots one by one. It’s flexible and allows me to design custom layouts for images.

Customize Image Sizes in Matplotlib Subplots (Python Methods)

Now let’s talk about controlling the size of images in subplots. This is where many beginners get stuck. Images may look stretched or too small if you don’t set the figure size correctly.

Here are the methods I use to handle this in Python.

Method 1 – Use the figsize Parameter

The easiest way to adjust image size is by using the figsize parameter in Matplotlib.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load images
img1 = mpimg.imread('usa_flag.png')
img2 = mpimg.imread('grandcanyon.jpg')

# Create subplots with custom figure size
fig, axes = plt.subplots(1, 2, figsize=(14, 6))

axes[0].imshow(img1)
axes[0].set_title("USA Flag")
axes[0].axis('off')

axes[1].imshow(img2)
axes[1].set_title("Grand Canyon")
axes[1].axis('off')

plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Display Images with Custom Sizes in Matplotlib Subplots

By increasing the figsize, I make sure my images look clear and professional. This is especially useful when preparing slides for a presentation.

Method 2 – Adjust Subplot Spacing with subplots_adjust()

Sometimes, even after setting figsize, the images may appear too close to each other. In such cases, I use subplots_adjust().

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load images
img1 = mpimg.imread('usa_flag.png')
img2 = mpimg.imread('newyork.jpg')
img3 = mpimg.imread('grandcanyon.jpg')

# Create subplots
fig, axes = plt.subplots(1, 3, figsize=(15, 5))

# Display images
axes[0].imshow(img1)
axes[0].set_title("USA Flag")
axes[0].axis('off')

axes[1].imshow(img2)
axes[1].set_title("New York")
axes[1].axis('off')

axes[2].imshow(img3)
axes[2].set_title("Grand Canyon")
axes[2].axis('off')

# Adjust spacing
plt.subplots_adjust(wspace=0.3, hspace=0.3)

plt.show()

You can see the output in the screenshot below.

Display Images in Matplotlib Subplots with Custom Sizes

This method gives me full control over the spacing between images. I often use it when preparing dashboards or visual reports for clients.

Method 3 – Use tight_layout() for Automatic Adjustment

If I don’t want to manually adjust spacing, I rely on tight_layout().

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load images
img1 = mpimg.imread('usa_flag.png')
img2 = mpimg.imread('newyork.jpg')

# Create subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 5))

axes[0].imshow(img1)
axes[0].set_title("USA Flag")
axes[0].axis('off')

axes[1].imshow(img2)
axes[1].set_title("New York")
axes[1].axis('off')

# Automatically adjust layout
plt.tight_layout()
plt.show()

This is my go-to method when I want a quick fix without worrying about manual adjustments. It saves time and works well in most cases.

Combine Both Images and Custom Sizes

In real-world projects, I often need to combine both techniques: displaying images in subplots and adjusting their sizes. Here’s a complete example that brings everything together.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Load images
img1 = mpimg.imread('usa_flag.png')
img2 = mpimg.imread('newyork.jpg')
img3 = mpimg.imread('grandcanyon.jpg')
img4 = mpimg.imread('washington.jpg')

# Create a 2x2 grid of subplots with custom size
fig, axes = plt.subplots(2, 2, figsize=(12, 10))

# Display images
axes[0, 0].imshow(img1)
axes[0, 0].set_title("USA Flag")
axes[0, 0].axis('off')

axes[0, 1].imshow(img2)
axes[0, 1].set_title("New York")
axes[0, 1].axis('off')

axes[1, 0].imshow(img3)
axes[1, 0].set_title("Grand Canyon")
axes[1, 0].axis('off')

axes[1, 1].imshow(img4)
axes[1, 1].set_title("Washington")
axes[1, 1].axis('off')

# Adjust layout automatically
plt.tight_layout()
plt.show()

This approach gives me the flexibility to design professional visualizations for reports, dashboards, or even academic publications.

Conclusion

Displaying images in Matplotlib subplots with custom sizes in Python may look tricky at first, but once you know the right methods, it becomes simple.

I’ve shown you how to use imshow() and add_subplot() to display images, and how to adjust sizes with figsize, subplots_adjust(), and tight_layout().

These techniques have helped me countless times in real-world Python projects, from preparing presentations for US-based clients to analyzing large image datasets.

You may also read other articles on Matplotlib:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.