Pass figsize=(width, height) in inches directly to plt.subplots(nrows, ncols, figsize=(width, height)) when creating the figure—or resize an already-created figure afterward using fig.set_size_inches(width, height) (or fig.set_figwidth(...) / fig.set_figheight(...)).
Basic Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
# 1. Recommended: Pass figsize=(width, height) in inches to plt.subplots()
fig, axes = plt.subplots(1, 2, figsize=(12, 4), layout="constrained")
axes[0].plot(x, np.sin(x))
axes[0].set_title("Sine Wave")
axes[1].plot(x, np.cos(x), color="tab:orange")
axes[1].set_title("Cosine Wave")
# 2. Optional: Resize an existing Figure object dynamically after creation
fig.set_size_inches(14, 5)
plt.show()
Key Details & Edge Cases
- Units are inches, not pixels (
pixels = inches * dpi):- Matplotlib’s default figure size is
(6.4, 4.8)inches at100DPI (640 × 480pixels). - To target an exact pixel resolution (e.g.,
1920 × 1080px), divide pixel dimensions bydpi:dpi = 150 fig, axes = plt.subplots(2, 2, figsize=(1920 / dpi, 1080 / dpi), dpi=dpi, layout="constrained")
- Matplotlib’s default figure size is
- Prevent overlapping subplot titles and axis labels (
layout="constrained"):- Changing
figsize(especially making multi-row or multi-column grids wider or shorter) often causes tick labels and subplot titles to collide. Passlayout="constrained"(or callfig.tight_layout()) so Matplotlib automatically recomputes subplot spacing to fit the new dimensions.
- Changing
- Avoid
plt.figure(figsize=...)beforeplt.subplots(...):- Calling
plt.figure(figsize=(10, 5))followed byfig, axes = plt.subplots(2, 2)creates two separate figures—leaving the first10 × 5figure blank and creating the subplots at the default6.4 × 4.8size. Always passfigsize=directly intoplt.subplots(..., figsize=(10, 5)).
- Calling
- Scaling
figsizeproportionally to grid rows and columns: For dynamic subplot grids where row/column counts vary, compute dimensions programmatically (e.g.,figsize=(4 * ncols, 3 * nrows)) so every individual panel keeps a consistent aspect ratio. - Changing global default figure dimensions across all plots: Update
rcParamsat the start of a script or notebook:plt.rcParams["figure.figsize"] = (10, 6) - Preserving
figsizewhen saving withplt.savefig(): Passingbbox_inches="tight"tofig.savefig("out.png", bbox_inches="tight")crops or expands the outer margins around labels, meaning the saved image’s pixel dimensions may differ slightly fromfigsize * dpi. Omitbbox_inches="tight"(and rely onlayout="constrained") if exact output pixel dimensions are required.
Comparison of Figure Resizing Methods in Matplotlib
| Method | Syntax | When Applied | Modifies Existing Figure? | Best Used When |
|---|---|---|---|---|
plt.subplots(..., figsize=...) |
fig, ax = plt.subplots(2, 2, figsize=(10, 6)) |
At figure creation | Creates new Figure |
Standard subplot workflows—cleanest single-call approach. |
fig.set_size_inches(w, h) |
fig.set_size_inches(12, 5) |
After figure creation | Yes (mutates fig) |
Resizing an existing figure returned by helper functions, Pandas .plot(), or Seaborn. |
fig.set_figwidth() / set_figheight() |
fig.set_figwidth(12) |
After figure creation | Yes (single axis only) | Adjusting width or height independently while keeping the other dimension unchanged. |
plt.rcParams["figure.figsize"] |
plt.rcParams["figure.figsize"] = (10, 5) |
Global session default | Affects subsequent plots | Setting a uniform default size across an entire Jupyter notebook or script. |
You must log in or register to comment.
