Skip to content

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 figsize when creating new figures
  • Adjusting sizes of existing figures
  • Using rcParams for 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

TaskBest MethodExample
Set size for a new figureplt.figure(figsize=(w, h))plt.figure(figsize=(8, 6))
Change existing figure sizefig.set_size_inches(w, h)fig.set_size_inches(12, 4)
Set global default sizeplt.rcParams["figure.figsize"] = ...(12, 6)
Set size in Pandas plotdf.plot(figsize=(w, h))(10, 5)
Use centimetersConvert cm → inchw_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

MethodWhen to UseExample
figure(figsize=...)Creating a brand-new figureMost recommended
set_size_inches()Resize an existing figureGood for auto-generated plots
plt.rcParams["figure.figsize"]Set global default inside a notebookMost common in EDA
matplotlib.rc("figure", figsize=...)Set global defaults in a scriptBest for production
Centimeters conversionAcademic, publisher guidelinesFor LaTeX papers
df.plot(figsize=...)Pandas workflowConvenient for quick EDA

⚠️ Troubleshooting: When figsize Doesn’t Work (Common Issues)

1. Jupyter Notebook ignores figsize

Fix:

%matplotlib inline

2. 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'] = False

5. 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:

PyGWalker for Data visualization (opens in a new tab)

Use it inside Jupyter:

pip install pygwalker
import pygwalker as pyg
 
gwalker = pyg.walk(df)

Or try it online:

Kaggle NotebookGoogle ColabGitHub ⭐
https://www.kaggle.com/asmdef/pygwalker-test (opens in a new tab)https://colab.research.google.com/drive/171QUQeq-uTLgSj1u-P9DQig7Md1kpXQ2 (opens in a new tab)https://github.com/Kanaries/pygwalker (opens in a new tab)

Frequently Asked Questions

  1. How do I set the figure size in Matplotlib? Use figure(figsize=(width, height)).

  2. How do I resize an existing figure? Use fig.set_size_inches(w, h).

  3. Can I change figure size in Pandas plots? Yes, via figsize=(w, h).