Question
How can I change the size of a figure created with Matplotlib in Python?
Short Answer
By the end of this page, you will understand how Matplotlib figure sizing works, how to set a figure's width and height, and when to use figsize, set_size_inches(), or global defaults with rcParams. You will also see practical examples and common mistakes to avoid.
Concept
Matplotlib draws plots inside a figure object. The figure is the outer container that holds one or more axes, titles, legends, and other visual elements.
A figure's size controls:
- how large the plot appears on screen
- how much room labels and titles have
- how large the saved image will be
- how readable the chart is in reports, notebooks, and apps
In Matplotlib, figure size is usually measured in inches. This often surprises beginners because screens use pixels. Matplotlib combines:
- figure size in inches, and
- DPI (dots per inch)
to determine the final pixel size.
For example:
figsize=(6, 4)means 6 inches wide and 4 inches tall- if
dpi=100, the output is about600 x 400pixels
The most common way to set figure size is when creating the figure:
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 5))
Or, if using the object-oriented style:
fig, ax = plt.subplots(figsize=(8, 5))
This matters in real programming because charts are often reused in:
- Jupyter notebooks
- dashboards
- reports and PDFs
- saved PNG/SVG images
- data analysis scripts
If you do not control the figure size, text may overlap, plots may look cramped, and exported images may not fit the place where you want to use them.
Mental Model
Think of a Matplotlib figure like a sheet of paper.
- The figure is the whole sheet.
- The axes are the drawing area on that sheet.
figsizechooses how big the sheet is.dpichooses how detailed the printing is.
A small sheet with lots of labels feels crowded. A larger sheet gives the content room to breathe.
So when you change figure size, you are not changing the data itself. You are changing the amount of space available to display that data clearly.
Syntax and Examples
Basic syntax
Using plt.figure()
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
plt.plot([1, 2, 3], [2, 4, 6])
plt.show()
figsize=(8, 4)means width 8 inches, height 4 inches.
Using plt.subplots()
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 5))
ax.plot([1, 2, 3], [1, 4, 9])
plt.show()
This is often preferred because it gives you direct access to the figure (fig) and axes (ax) objects.
Changing size after creation
import matplotlib.pyplot plt
fig, ax = plt.subplots()
ax.plot([, , ], [, , ])
fig.set_size_inches(, )
plt.show()
Step by Step Execution
Consider this example:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 3))
ax.plot([1, 2, 3], [2, 4, 3])
ax.set_title('Sales Trend')
plt.show()
Step by step:
-
import matplotlib.pyplot as plt- Loads Matplotlib's plotting tools.
-
fig, ax = plt.subplots(figsize=(6, 3))- Creates a new figure and one axes.
- The figure is 6 inches wide and 3 inches tall.
-
ax.plot([1, 2, 3], [2, 4, 3])- Draws a line on the axes.
-
ax.set_title('Sales Trend')- Adds a title inside the figure area.
-
plt.show()- Displays the figure.
What if the plot looks too compressed?
You can increase the height:
Real World Use Cases
Figure sizing is important in many practical situations:
Jupyter notebooks
In notebooks, the default figure size may be too small for long labels or multiple lines. Increasing the size improves readability.
fig, ax = plt.subplots(figsize=(10, 4))
Reports and presentations
If a chart must fit into a slide or PDF page, you often choose a specific width and height so the chart matches the layout.
Saving images for websites
When exporting charts as PNG files, you may want a wide banner-style chart or a square chart depending on where it will appear.
Dashboards
A dashboard may contain several small charts side by side. You need consistent figure sizes so the layout looks clean.
Data-heavy charts
Plots with many tick labels, categories, or subplots often need larger figures to avoid overlapping text.
Real Codebase Usage
In real projects, developers usually do more than just call figsize once.
Common patterns
1. Set size during creation
This is the most common pattern because it keeps plot creation predictable.
fig, ax = plt.subplots(figsize=(8, 4))
2. Store standard sizes in configuration
Teams often define standard chart sizes so all plots look consistent.
DEFAULT_FIGSIZE = (8, 5)
fig, ax = plt.subplots(figsize=DEFAULT_FIGSIZE)
3. Use different sizes for different chart types
For example:
- line charts: wide format
- correlation plots: square format
- multi-panel plots: taller format
LINE_CHART_SIZE = (10, 4)
HEATMAP_SIZE = (6, 6)
4. Resize before saving
Sometimes a plot is created first and resized later depending on export requirements.
fig.set_size_inches(12, 6)
fig.savefig('report_chart.png', dpi=150)
Common Mistakes
1. Confusing inches with pixels
Beginners often expect figsize=(800, 600) to mean pixels.
# Incorrect idea
plt.figure(figsize=(800, 600))
This creates an enormous figure because Matplotlib interprets those values as inches.
Use inches instead:
plt.figure(figsize=(8, 6), dpi=100)
2. Forgetting that DPI affects saved image size
Two figures with the same figsize can produce different pixel dimensions when saved.
fig, ax = plt.subplots(figsize=(6, 4), dpi=100)
fig.savefig('a.png')
fig, ax = plt.subplots(figsize=(6, 4), dpi=200)
fig.savefig('b.png')
The second image will usually have more pixels.
3. Changing the wrong object
If you are using fig, ax = plt.subplots(), resize the figure object, not the axes.
fig, ax = plt.subplots()
fig.set_size_inches(, )
Comparisons
| Approach | When to use | Example | Notes |
|---|---|---|---|
plt.figure(figsize=(w, h)) | When creating a simple figure directly | plt.figure(figsize=(8, 4)) | Good for quick plotting |
plt.subplots(figsize=(w, h)) | When creating figure and axes together | fig, ax = plt.subplots(figsize=(8, 4)) | Most common in modern Matplotlib code |
fig.set_size_inches(w, h) | When resizing an existing figure | fig.set_size_inches(8, 4) | Useful after creation |
plt.rcParams['figure.figsize'] = (...) | When setting a session-wide default |
Cheat Sheet
Quick reference
Create a figure with a specific size
plt.figure(figsize=(8, 4))
Create figure and axes with a specific size
fig, ax = plt.subplots(figsize=(8, 4))
Resize an existing figure
fig.set_size_inches(8, 4)
Set default size for future figures
plt.rcParams['figure.figsize'] = (10, 6)
Save with a chosen DPI
fig.savefig('plot.png', dpi=200)
Rules to remember
figsizeis in inches, not pixels.- Pixel size is approximately
width * dpibyheight * dpi. plt.subplots(figsize=(w, h))is the most common modern approach.
FAQ
How do I change the size of a Matplotlib plot?
Use figsize when creating the figure:
fig, ax = plt.subplots(figsize=(8, 4))
Is Matplotlib figsize measured in pixels?
No. figsize is measured in inches. The final pixel size depends on the DPI.
How do I make a Matplotlib figure bigger after creating it?
Use:
fig.set_size_inches(8, 4)
What is the default figure size in Matplotlib?
It depends on your Matplotlib settings, but a common default is around 6.4 x 4.8 inches.
How do I set the same figure size for all plots?
Use rcParams:
plt.rcParams['figure.figsize'] = (10, 6)
Why does my saved image size look different from the on-screen plot?
Because saved image dimensions also depend on DPI. The screen display and file export may not match exactly.
Should I use or ?
Mini Project
Description
Build a small chart script that compares monthly sales values and exports the result as an image. This project demonstrates how figure size affects readability and saved output quality.
Goal
Create a Matplotlib line chart with a custom figure size and save it to a PNG file.
Requirements
[ "Create a list of months and a list of sales values.", "Plot the data as a line chart using Matplotlib.", "Set a custom figure size when creating the chart.", "Add a title and axis labels.", "Save the chart as a PNG file." ]
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.