Question

How to Change Figure Size in Matplotlib (Python)

pythonpandasmatplotlibseabornfigsize

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 about 600 x 400 pixels

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.
  • figsize chooses how big the sheet is.
  • dpi chooses 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.

Take Quiz

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 as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [3, 2, 5])
fig.set_size_inches(9, 4)
plt.show()

This is useful if the figure already exists and you want to resize it later.

Setting a global default size

import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = (10, 6)

plt.plot([1, 2, 3], [2, 3, 4])
plt.show()

After this, newly created figures will use that default size unless you override it.

Example with DPI

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6, 4), dpi=150)
ax.plot([1, 2, 3], [2, 5, 3])
plt.show()

Approximate output size in pixels:

  • width: 6 * 150 = 900
  • height: 4 * 150 = 600

Saving with a chosen size

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(8, 5))
ax.plot([1, 2, 3], [10, 20, 15])
fig.savefig('chart.png', dpi=200)

Here the saved image size depends on both figsize and dpi.

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:

  1. import matplotlib.pyplot as plt

    • Loads Matplotlib's plotting tools.
  2. fig, ax = plt.subplots(figsize=(6, 3))

    • Creates a new figure and one axes.
    • The figure is 6 inches wide and 3 inches tall.
  3. ax.plot([1, 2, 3], [2, 4, 3])

    • Draws a line on the axes.
  4. ax.set_title('Sales Trend')

    • Adds a title inside the figure area.
  5. plt.show()

    • Displays the figure.

What if the plot looks too compressed?

You can increase the height:

fig, ax = plt.subplots(figsize=(6, 5))

Now the same content has more vertical space, so the title, labels, and line are easier to read.

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.

Take Quiz

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)

5. Combine with layout helpers

When a figure contains many labels, legends, or subplots, developers often use sizing together with layout tools such as tight_layout().

fig, ax = plt.subplots(figsize=(9, 5))
ax.plot([1, 2, 3], [4, 1, 5])
fig.tight_layout()

This helps prevent clipping and overlap.

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(8, 4)

4. Setting global defaults unintentionally

plt.rcParams['figure.figsize'] = (12, 8)

This affects future figures in the current session. That can be helpful, but it can also confuse you if later plots seem unexpectedly large.

5. Assuming larger is always better

A bigger figure is not always clearer. For simple charts, too much empty space can make the chart look awkward. Choose a size that matches the content.

6. Ignoring layout issues

Even with a good figure size, labels may still overlap.

fig, ax = plt.subplots(figsize=(6, 4))
ax.set_title('A Very Long Title That Might Get Cut Off')
fig.tight_layout()

Use tight_layout() or adjust spacing when needed.

Comparisons

ApproachWhen to useExampleNotes
plt.figure(figsize=(w, h))When creating a simple figure directlyplt.figure(figsize=(8, 4))Good for quick plotting
plt.subplots(figsize=(w, h))When creating figure and axes togetherfig, ax = plt.subplots(figsize=(8, 4))Most common in modern Matplotlib code
fig.set_size_inches(w, h)When resizing an existing figurefig.set_size_inches(8, 4)Useful after creation
plt.rcParams['figure.figsize'] = (...)When setting a session-wide defaultplt.rcParams['figure.figsize'] = (10, 6)Affects future plots

figsize vs dpi

PropertyControlsExample
figsizePhysical size of the figure in inches(8, 5)
dpiResolution or pixel density100, 150, 300

You usually choose both when you care about exported image quality.

plt.figure() vs plt.subplots()

FunctionBest forReturns
plt.figure()Simple figure creationFigure only
plt.subplots()Most plotting workflowsFigure and axes
Take Quiz

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

  • figsize is in inches, not pixels.
  • Pixel size is approximately width * dpi by height * dpi.
  • plt.subplots(figsize=(w, h)) is the most common modern approach.
  • Use fig.set_size_inches() if the figure already exists.
  • Use tight_layout() if labels or titles are clipped.

Handy examples

fig, ax = plt.subplots(figsize=(12, 4))   # wide chart
fig, ax = plt.subplots(figsize=(6, 6))    # square chart
fig, ax = plt.subplots(figsize=(5, 8))    # tall chart

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 plt.figure() or plt.subplots()?

For most cases, plt.subplots() is preferred because it returns both the figure and axes objects and fits better with Matplotlib's object-oriented style.

Take Quiz

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." ]

Take Quiz

Keep learning

Related questions

View more