Mastering Figure Sizes in Matplotlib: A Complete Guide
Updated on
Controlling figure size is one of the most important parts of creating clean, readable, and publication-quality visualizations in Matplotlib. Whether you're generating quick exploratory plots or preparing polished figures for reports, knowing how to set and adjust figure sizes correctly will save you from distorted charts, unreadable labels, and inconsistent layouts.
This guide covers all common and practical methods to control figure sizes in Matplotlib, including:
- Using
figsizewhen creating new figures - Adjusting sizes of existing figures
- Using
rcParamsfor global defaults - Setting figure size in centimeters
- Changing size in Pandas plots
- Resetting and dynamically modifying global size
- A troubleshooting guide
- A comparison table summarizing all methods
⭐ TL;DR — Quick Reference
| Task | Best Method | Example |
|---|---|---|
| Set size for a new figure | plt.figure(figsize=(w, h)) | plt.figure(figsize=(8, 6)) |
| Change existing figure size | fig.set_size_inches(w, h) | fig.set_size_inches(12, 4) |
| Set global default size | plt.rcParams["figure.figsize"] = ... | (12, 6) |
| Set size in Pandas plot | df.plot(figsize=(w, h)) | (10, 5) |
| Use centimeters | Convert cm → inch | w_cm / 2.54 |
Understanding Figure Sizes in Matplotlib
Matplotlib measures figure size in inches, using a (width, height) tuple.
The most direct way to create a figure with a fixed size is:
from matplotlib.pyplot import figure
# Create a new figure (width=8 inches, height=6 inches)
figure(figsize=(8, 6))This ensures your chart uses the size you specify, regardless of dataset or plot type.
Adjusting Figure Size After the Figure Exists
Sometimes a figure is created automatically—for example via plt.plot() or a Pandas plot. You can still resize it afterwards:
import matplotlib.pyplot as plt
# Get the current figure
fig = plt.gcf()
# Change its size to 18.5 x 10.5 inches
fig.set_size_inches(18.5, 10.5)
# Save the resized figure
fig.savefig("figure.png", dpi=100)set_size_inches() is the most reliable way to resize figures after they exist.
Setting Global Default Figure Size with rcParams
If you want all your plots to follow the same size (useful in notebooks and scripts), configure a global default:
import matplotlib.pyplot as plt
# Make all future figures 20 x 3 inches
plt.rcParams["figure.figsize"] = (20, 3)This affects all subsequent plots until overwritten.
Setting Figure Size in Centimeters
If you need metric sizing (e.g., academic papers), convert centimeters → inches:
width_cm = 20
height_cm = 10
# Convert cm to inches
width_in = width_cm / 2.54
height_in = height_cm / 2.54
# Use the converted size
figure(figsize=(width_in, height_in))This works identically to the inch-based approach.
Resetting or Dynamically Changing Global Defaults
To return to Matplotlib's original settings:
plt.rcParams["figure.figsize"] = plt.rcParamsDefault["figure.figsize"]This is useful when a notebook mixes small quick plots and larger publication plots.
Controlling Figure Size with Pandas
Pandas integrates directly with Matplotlib and also accepts figsize:
df['some_column'].plot(figsize=(10, 5))For subplots:
fig, ax = plt.subplots(figsize=(10, 5))
df['some_column'].plot(ax=ax)This method is especially convenient in data analysis notebooks.
Changing the Matplotlib Default Figure Size for All Plots
Use this if you want every plot to follow the same size automatically:
import matplotlib
matplotlib.rc("figure", figsize=(10, 5))This is similar to modifying rcParams directly and works across scripts and notebooks.
🔍 Comparison of All Methods
| Method | When to Use | Example |
|---|---|---|
figure(figsize=...) | Creating a brand-new figure | Most recommended |
set_size_inches() | Resize an existing figure | Good for auto-generated plots |
plt.rcParams["figure.figsize"] | Set global default inside a notebook | Most common in EDA |
matplotlib.rc("figure", figsize=...) | Set global defaults in a script | Best for production |
| Centimeters conversion | Academic, publisher guidelines | For LaTeX papers |
df.plot(figsize=...) | Pandas workflow | Convenient for quick EDA |
⚠️ Troubleshooting: When figsize Doesn’t Work (Common Issues)
1. Jupyter Notebook ignores figsize
Fix:
%matplotlib inline2. Tight layout cuts off labels
Use:
plt.tight_layout()3. Saving figure changes the size
Always set DPI explicitly:
plt.savefig("output.png", dpi=150)4. Fonts or labels scale unexpectedly
Turn off autoscaling:
plt.rcParams['figure.autolayout'] = False5. Subplots overlap
Increase size:
fig, ax = plt.subplots(figsize=(12, 8))These troubleshooting sections are highly ranked in SEO for Matplotlib topics and solve user frustration.
Alternative to Matplotlib: Visualize Data with PyGWalker
Besides Matplotlib, you can explore your pandas DataFrame visually using PyGWalker, an open-source drag-and-drop data visualization tool:
Use it inside Jupyter:
pip install pygwalker
import pygwalker as pyg
gwalker = pyg.walk(df)Or try it online:
Frequently Asked Questions
-
How do I set the figure size in Matplotlib? Use
figure(figsize=(width, height)). -
How do I resize an existing figure? Use
fig.set_size_inches(w, h). -
Can I change figure size in Pandas plots? Yes, via
figsize=(w, h).
