How do you adjust the figure size of Seaborn axes-level and figure-level plots? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you adjust the figure size of Seaborn axes-level and figure-level plots?

message-square
0

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 on sns.relplot(), sns.catplot(), sns.displot(), and sns.lmplot():
    • Figure-level functions instantiate their own internal FacetGrid, JointGrid, or PairGrid figure—leaving your pre-created plt.figure() blank and sizing the plot solely from height (default 5 inches) and aspect (default 1).
    • Conversely, axes-level functions (sns.lineplot, sns.barplot, sns.heatmap, etc.) do not accept height= or aspect= arguments (passing them raises TypeError: got an unexpected keyword argument 'height') and draw onto the active Matplotlib Axes instead.
  • height and aspect define 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 is 5.25 × 3.5 inches, so a 2×2 grid produces a figure roughly 10.5 × 7 inches (plus legend margin).
  • Resizing an existing figure-level FacetGrid / PairGrid after 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) (alias g.fig.set_size_inches(12, 6)) followed by g.tight_layout().
  • 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)}) (or plt.rcParams["figure.figsize"] = (10, 5)). Note that this affects axes-level plots only; figure-level functions still default to height=5, aspect=1.
  • Subtle sizing differences in sns.jointplot() and sns.pairplot():
    • sns.jointplot(..., height=6, ratio=5) accepts height (making a square 6 × 6 inch figure) and ratio (main plot vs. marginal axes ratio), but does not accept aspect.
    • sns.pairplot(..., height=2.5, aspect=1.2) accepts both height and aspect per 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