Display Images in Matplotlib Subplots with Custom Sizes

If you’ve tried putting photos in a subplot grid, you’ve probably seen tiny pictures floating in huge white gaps. In this tutorial, I’ll display images in Matplotlib subplots with imshow() on each axes, then size them with figsize and width_ratios.

Those gaps appear because imshow() protects the picture’s shape, so a wide photo can’t fill a square slot.

Everything below ran on Matplotlib 3.11.2 on Python 3.12.5.

The sample images we’ll use

I’m using four generated PNGs named after US cities, each a different shape. Denver is landscape, Austin is portrait, Seattle is very wide, and Boston is square. The yellow circle is the useful part: if it turns oval, the image got stretched.

import numpy as np
import matplotlib.pyplot as plt

SIZES = {"denver": (800, 600), "austin": (600, 800),
         "seattle": (1200, 400), "boston": (500, 500)}

for city, (w, h) in SIZES.items():
    yy, xx = np.mgrid[0:h, 0:w]
    img = np.dstack([xx / w, yy / h, np.full((h, w), 0.6)])
    circle = (xx - w / 2) ** 2 + (yy - h / 2) ** 2 < (0.35 * min(w, h)) ** 2
    img[circle] = (1.0, 0.85, 0.2)
    plt.imsave(f"{city}.png", img)
    print(f"{city}.png", plt.imread(f"{city}.png").shape)

Output:

denver.png (600, 800, 4)
austin.png (800, 600, 4)
seattle.png (400, 1200, 4)
boston.png (500, 500, 4)
Command Prompt output listing the four sample PNG images and their array shapes in height, width and channels
Each file reads back as a (height, width, 4) array.

Note that shape lists height first. If you’re new to grids, start with my Matplotlib subplot tutorial.

Method 1: Display images in a subplot grid with axes.flat (Recommended)

Use this when the images are similar in size. Same four cities, one imshow() per axes:

import matplotlib.pyplot as plt

cities = ["denver", "austin", "seattle", "boston"]
fig, axes = plt.subplots(2, 2, figsize=(9, 6), layout="constrained")

for ax, city in zip(axes.flat, cities):
    img = plt.imread(f"{city}.png")
    ax.imshow(img)
    ax.set_title(f"{city.title()} ({img.shape[1]} x {img.shape[0]})")
    ax.axis("off")

plt.show()
Matplotlib plot window showing four city images of different sizes in a two by two subplot grid with titles
All four keep their shape, so Seattle sits inside a thin band of its slot.

How does this code work? plt.subplots(2, 2) returns a 2D array of axes, and axes.flat lets one loop walk it.

ax.axis("off") removes the pixel ticks, and layout="constrained" keeps titles from colliding. For more control over the gaps, see spacing between Matplotlib subplots.

Method 2: Give each image a custom width with width_ratios

Use this for different shapes in one tidy row. Same files, minus portrait Austin. Each column’s width comes from the image’s own aspect ratio:

import matplotlib.pyplot as plt

cities = ["denver", "seattle", "boston"]
imgs = [plt.imread(f"{c}.png") for c in cities]
ratios = [i.shape[1] / i.shape[0] for i in imgs]
height = 1.7

fig, axes = plt.subplots(1, 3, width_ratios=ratios, layout="constrained",
                         figsize=(sum(ratios) * height, height))
for ax, img, city in zip(axes, imgs, cities):
    ax.imshow(img)
    ax.set_title(city.title())
    ax.axis("off")

print("ratios:", [round(r, 2) for r in ratios])
print("figsize:", [round(float(v), 2) for v in fig.get_size_inches()])
plt.show()

Output:

ratios: [1.33, 3.0, 1.0]
figsize: [9.07, 1.7]
Matplotlib plot window with three images in one row at equal height using width ratios from each image aspect ratio
With ratios of 1.33, 3.0 and 1.0, all three images share one height.

How does this code work? Width divided by height gives 1.33, 3.0 and 1.0. Those become the column ratios, and multiplying their sum by 1.7 inches gives a 9.07-inch figure with no leftover space.

General figure sizing, without images, is in my guide to Matplotlib subplot figure size. To make one image span several slots, use plt.subplot_mosaic("AB;CC") or GridSpec layouts.

Method 3: Show an image at its exact pixel size

Use this when pixels must stay 1:1. Same Seattle file, but the figure is built from its dimensions:

import matplotlib.pyplot as plt

img = plt.imread("seattle.png")
h, w = img.shape[:2]
dpi = 100

fig = plt.figure(figsize=(w / dpi, h / dpi), dpi=dpi)
ax = fig.add_axes([0, 0, 1, 1])                 # fill the whole figure
ax.imshow(img)
ax.axis("off")
fig.savefig("seattle_copy.png", dpi=dpi)

print("original:", img.shape)
print("saved:   ", plt.imread("seattle_copy.png").shape)

Output:

original: (400, 1200, 4)
saved:    (400, 1200, 4)
Command Prompt output comparing the original and saved image array shapes, both 400 by 1200 pixels
The saved copy has the same 400 x 1200 pixels as the source.

How does this code work? Inches times DPI equals pixels, so dividing 1200 and 400 by 100 gives a 12 x 4 inch figure. The axes fills it, leaving no margin to shrink the image.

If your copy comes out empty, here’s how to fix a blank Matplotlib savefig.

Why do images look stretched or tiny in subplots?

It’s almost always the aspect argument. Here’s Seattle both ways:

import matplotlib.pyplot as plt

img = plt.imread("seattle.png")
fig, axes = plt.subplots(1, 2, figsize=(9, 4), layout="constrained")

axes[0].imshow(img, aspect="equal")
axes[0].set_title('aspect="equal" (default)')
axes[1].imshow(img, aspect="auto")
axes[1].set_title('aspect="auto"')
plt.show()
Matplotlib plot window comparing imshow aspect equal and aspect auto on the same wide image, with the circle stretched on the right
The default keeps the circle round. With aspect="auto" it fills the axes and turns oval.

Important: keep the default for photos. Reserve "auto" for heatmaps, where filling the axes matters more than shape.

Which image subplot method should you use?

GoalUse
Similar-sized imagesaxes.flat loop
Mixed shapes, one rowwidth_ratios
One image spanning slotssubplot_mosaic or GridSpec
True pixel sizefigsize = pixels / dpi

Things to Keep in Mind

  • Grayscale arrays look purple until you pass cmap="gray".
  • Extra grid cells stay visible, so call ax.axis("off") on unused ones.
  • If margins refuse to change, check why subplots_adjust stops working.

These related Matplotlib guides pick up where the image grid leaves off:

Frequently asked questions

How do I display multiple images in Matplotlib subplots?

Create axes with plt.subplots(), loop over axes.flat, and call ax.imshow(img).

How do I make one subplot image bigger?

Pass width_ratios or use subplot_mosaic so it spans slots.

Why is there white space around my images?

imshow() preserves shape. Match column widths to each image’s ratio.

How do I remove axes from an image subplot?

Call ax.axis("off") on that axes.

How do I show an image at actual size?

Set figsize to pixels divided by DPI and use a full-figure axes.

Where is imshow documented?

See Axes.imshow in the Matplotlib docs.