Pass figsize=(width, height) (in inches) to plt.subplots(figsize=...) or plt.figure(figsize=...) before calling axes-level Seaborn functions (sns.scatterplot, sns.histplot, sns.boxplot, sns.heatmap), whereas for figure-level functions (sns.relplot, sns.displot, sns.catplot, sns.lmplot, sns.jointplot, sns.pairplot), pass height (height of each facet in inches) and aspect (width = height * aspect) directly into the Seaborn function call—ignoring plt.figure(figsize=...), which figure-level functions overwrite by creating their own FacetGrid figure.
Basic Example
import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset("tips")
# 1. Axes-level plot (scatterplot, histplot, boxplot, heatmap, barplot):
# Create figure/axes FIRST with figsize=(width, height) in inches, then pass ax=ax
fig, ax = plt.subplots(figsize=(10, 5))
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time", ax=ax)
ax.set_title("Axes-Level Plot (10x5 inches)")
plt.tight_layout()
plt.show()
# 2. Figure-level plot (relplot, displot, catplot, lmplot):
# Pass height (per facet, in inches) and aspect (width / height ratio) directly
g = sns.relplot(
data=tips,
x="total_bill",
y="tip",
col="time", # Creates 2 side-by-side facets
height=4, # Each facet is 4 inches tall
aspect=1.25, # Each facet is 4 * 1.25 = 5 inches wide (total width ≈ 10 in)
)
g.fig.suptitle("Figure-Level Plot (2 facets × 5x4 in)", y=1.03)
plt.show()
Key Details & Edge Cases
- Why
plt.figure(figsize=(12, 6))does nothing onsns.relplot(),sns.catplot(),sns.displot(), andsns.lmplot():- Figure-level functions instantiate their own internal
FacetGrid,JointGrid, orPairGridfigure—leaving your pre-createdplt.figure()blank and sizing the plot solely fromheight(default5inches) andaspect(default1). - Conversely, axes-level functions (
sns.lineplot,sns.barplot,sns.heatmap, etc.) do not acceptheight=oraspect=arguments (passing them raisesTypeError: got an unexpected keyword argument 'height') and draw onto the active MatplotlibAxesinstead.
- Figure-level functions instantiate their own internal
heightandaspectdefine the size of EACH individual subplot facet, NOT the entire figure:- In
sns.catplot(..., col="day", col_wrap=2, height=3.5, aspect=1.5), each subplot panel is5.25 × 3.5inches, so a 2×2 grid produces a figure roughly10.5 × 7inches (plus legend margin).
- In
- Resizing an existing figure-level
FacetGrid/PairGridafter creation (g.fig.set_size_inches):- If you need to override the final outer dimensions after calling a figure-level function, call
g.figure.set_size_inches(12, 6)(aliasg.fig.set_size_inches(12, 6)) followed byg.tight_layout().
- If you need to override the final outer dimensions after calling a figure-level function, call
- Setting a global default figure size across all axes-level plots:
- Configure Matplotlib’s rcParams once at the top of your script or notebook via
sns.set_theme(rc={"figure.figsize": (10, 5)})(orplt.rcParams["figure.figsize"] = (10, 5)). Note that this affects axes-level plots only; figure-level functions still default toheight=5, aspect=1.
- Configure Matplotlib’s rcParams once at the top of your script or notebook via
- Subtle sizing differences in
sns.jointplot()andsns.pairplot():sns.jointplot(..., height=6, ratio=5)acceptsheight(making a square6 × 6inch figure) andratio(main plot vs. marginal axes ratio), but does not acceptaspect.sns.pairplot(..., height=2.5, aspect=1.2)accepts bothheightandaspectper grid cell.
Comparison of Figure Sizing Methods Across Seaborn Plot Types
| Seaborn Plot Category | Representative Functions | How to Set Size | Formula / Dimensions | Accepts ax= Parameter? |
|---|---|---|---|---|
| Axes-Level Plots | scatterplot, lineplot, histplot, kdeplot, boxplot, violinplot, barplot, heatmap, regplot |
fig, ax = plt.subplots(figsize=(W, H)) + ax=ax (or sns.set_theme(rc={"figure.figsize": (W, H)})) |
Exact total figure width W × height H (in inches) |
Yes |
Figure-Level (FacetGrid) |
relplot, displot, catplot, lmplot |
Pass height=H, aspect=A directly to function (or g.figure.set_size_inches(W, H)) |
Each facet is (H * A) wide × H tall (in inches) |
No (TypeError) |
Figure-Level (PairGrid) |
pairplot |
Pass height=H, aspect=A directly to sns.pairplot() |
Each grid cell is (H * A) wide × H tall (in inches) |
No |
Figure-Level (JointGrid) |
jointplot |
Pass height=H directly to sns.jointplot() |
Square figure H × H inches (no aspect parameter) |
No |
You must log in or register to comment.
