Python
Matplotlib
Data Visualization
Subplots
Plot Customization

How do I change the figure size with subplots?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When you create subplots in Matplotlib, figure size is controlled at the figure level, not separately for each axis. The usual answer is to pass figsize=(width, height) to plt.subplots, or to change the size later with fig.set_size_inches.

Set the Size When You Create the Figure

The most common pattern is to define the size up front:

python
1import matplotlib.pyplot as plt
2
3fig, axes = plt.subplots(2, 2, figsize=(10, 6))
4
5for ax in axes.flat:
6    ax.plot([1, 2, 3], [1, 4, 9])
7
8plt.show()

The figsize values are in inches. Matplotlib combines those dimensions with the figure DPI to determine the final pixel size when rendering or saving.

This is usually the cleanest solution because layout engines such as tight_layout or constrained_layout can work with the correct geometry from the start.

Change the Size After Creation

If you already have a figure object, resize it later:

python
1import matplotlib.pyplot as plt
2
3fig, axes = plt.subplots(1, 3)
4fig.set_size_inches(12, 4)
5
6for ax in axes:
7    ax.bar([1, 2, 3], [3, 1, 2])
8
9plt.show()

This is useful when figure size depends on data or when you are writing helper functions that receive an existing figure.

Understand the Relationship Between Size and Layout

Changing figure size does not automatically fix overlap. If subplot titles, tick labels, or legends are colliding, also use a layout tool:

python
1import matplotlib.pyplot as plt
2
3fig, axes = plt.subplots(2, 1, figsize=(8, 6), constrained_layout=True)
4
5axes[0].set_title("First subplot")
6axes[1].set_title("Second subplot")
7
8plt.show()

constrained_layout=True is often the better default for new code. Older examples frequently use plt.tight_layout(), which still works well in many cases:

python
plt.tight_layout()

Think of figure size and layout as related but different knobs. A bigger figure gives elements more room, while the layout engine decides how that room is distributed.

Save the Figure at the Intended Output Size

If the plot looks right on screen but wrong in the saved file, check both figsize and DPI:

python
fig, ax = plt.subplots(figsize=(8, 4), dpi=150)
ax.plot([0, 1, 2], [0, 1, 0])
fig.savefig("plot.png")

An 8 x 4 inch figure at 150 DPI produces a 1200 x 600 pixel image. That matters if the figure is destined for a slide deck, a notebook, or a printed document.

Pick Sizes Deliberately

There is no universal best figure size. A few practical defaults help:

  • use something like figsize=(6, 4) for a single small chart
  • use wider figures such as figsize=(10, 4) for side-by-side subplots
  • increase height for stacked plots, especially when labels are long

If the subplot grid grows, increase the figure size proportionally instead of expecting the axes to stay readable at the old size.

Common Pitfalls

  • Trying to resize subplots individually instead of changing the figure size. The axes live inside the figure.
  • Forgetting that figsize is measured in inches, not pixels.
  • Enlarging the figure without addressing layout overlap. Use constrained_layout or tight_layout when labels collide.
  • Saving at an unexpected DPI and then wondering why the exported image is too small or too blurry.
  • Creating a dense grid of subplots without increasing width and height proportionally.

Summary

  • Pass figsize=(w, h) to plt.subplots to set the figure size up front.
  • Use fig.set_size_inches when you need to resize an existing figure.
  • Combine figure sizing with constrained_layout or tight_layout for readable spacing.
  • Remember that output size depends on both inches and DPI.
  • Scale the figure with the complexity of the subplot grid instead of relying on defaults.

Course illustration
Course illustration

All Rights Reserved.