Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 57 ยท Getting Started

Figure Size, DPI and Layout

Control Matplotlib figure size with figsize and dpi, see how they become pixels, and fix clipped labels with constrained or tight layout and margins.

Two numbers decide how big a Matplotlib figure is: its size in inches and its dots per inch (DPI). Multiply them and you get pixels. A third topic, layout, decides how the plot, labels and titles share the space so nothing is cut off or wasted. These three together explain most "my figure looks wrong" questions: text too small, too much white margin, labels chopped at the edge.

You will set sizes the object-oriented way, as recommended in pyplot vs the Object-Oriented API.

figsize: width and height in inches

figsize=(width, height) is given in inches. The default in Matplotlib 3.10 is (6.4, 4.8). Why inches? Because figures were designed for print, where size matters. Changing the size changes the shape (aspect ratio) of the chart and the relative size of the text: a bigger figure makes fonts look smaller in comparison.

Python Same data, three shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here every panel has the same data and the same fonts. Remember: the figsize applies to the Figure, not to an Axes. A figure sized (11, 3) holds all three plots.

A good habit is to choose figsize for where the chart will be used: about (6, 4) for a web article, (10, 5.6) for a widescreen slide, and (3.3, 2.5) for one column of a journal page.

DPI: dots per inch

dpi says how many pixels fit in one inch. The pixel dimensions of the image are:

width in pixels = figsize width ร— dpi, and height in pixels = figsize height ร— dpi

The default dpi for figures on screen is 100. The default for saving is also 100 in current versions, and you can set it explicitly with savefig(dpi=...). A (6, 4) figure at 100 dpi is 600 ร— 400 pixels; at 200 dpi it is 1200 ร— 800 with the same layout and proportions, just sharper.

Python From inches to pixels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

DPI changes the sharpness, not the proportions. Inches decide the layout; DPI decides how finely it is sampled. This is why increasing dpi is the standard way to make a PNG crisp for print, while changing figsize changes how big the text looks relative to the plot. Saving at different DPI values is covered in Saving Figures.

Size = inches, sharpness = DPI

figsize Width and height in inches. Sets shape and text scale.
dpi Pixels per inch. Sets sharpness.
Pixels inches times dpi.

Vector files like SVG and PDF ignore dpi for shapes.

1 / 2

Resizing a figure that already exists

figsize is read when the Figure is created, but you can change the size afterwards. Three methods do it, and a fourth reads the result:

Method What it does
fig.set_figwidth(w) changes only the width, in inches
fig.set_figheight(h) changes only the height, in inches
fig.set_size_inches(w, h) changes both (a pair (w, h) also works)
fig.get_size_inches() returns a NumPy array [width, height]

All three setters take forward=True (the default), which also resizes the window of a GUI backend; pass forward=False to change the Figure's size without touching the window. The same functionality is available in pyplot style through plt.gcf().set_size_inches(...). Resizing after drawing is convenient when a helper function created the figure for you, and you want a different shape for a slide or a column of a paper. The pixel size is always inches * dpi.

Python Resize a Figure after creating it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last call wins, so the chart is 8 by 3 inches, which is 800 by 300 pixels at 100 dpi. If you use constrained_layout or tight_layout, the Axes follow the new size automatically; with manual subplots_adjust values you may need to adjust them again.

Layout: giving everything room

By default Matplotlib leaves fixed margins around each plot. That is fine when labels are short, but long tick labels, big titles or multi-line text can be clipped or can overlap neighboring plots.

Python Clipped labels, then fixed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The usual fixes are two layout engines, which compute the margins for you.

  • tight: a one-shot call, fig.tight_layout(), which resizes the plot area so labels and titles fit. It runs once, when you call it.
  • constrained: switched on with layout="constrained" when you create the figure. It re-solves the layout each time the figure is drawn, and handles colorbars, suptitles and legends better. It is the recommended choice for new code.
Python layout=constrained

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try replacing layout="constrained" with nothing in the code above and you will see overlapping labels. Choose one engine per figure; switching between them in the middle of a script can give surprising results.

Margins and aspect ratio

Two more layout ideas you will meet often:

  • Margins around the data: ax.margins(0.05) adds five percent of padding inside the axes, so lines do not touch the frame. Set ax.margins(0) for data that should fill the plot area exactly (an image or a heatmap).
  • Aspect ratio: ax.set_aspect("equal") makes one unit on x the same length as one unit on y. Circles then look like circles. It is covered in Limits, Scales and Aspect.
Python A circle that looks like a circle

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Setting your own defaults

If every chart needs the same size and dpi, change the defaults once with rcParams, instead of repeating figsize= in each call:

Python
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (7, 4)
plt.rcParams["figure.dpi"] = 120
plt.rcParams["figure.constrained_layout.use"] = True

rcParams and styles are the subject of Styles, rcParams and Themes.

Common problems and fixes

  • Labels clipped at the edge: use layout="constrained", or call fig.tight_layout().
  • Lots of blank margin around a saved file: save with bbox_inches="tight" (see Saving Figures).
  • Text looks tiny: the figure is too large for the font size. Reduce figsize, or increase fontsize.
  • Blurry PNG: increase dpi when saving, not figsize.
  • Circle looks like an oval: set ax.set_aspect("equal").

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A figure has figsize=(5, 3) and dpi=100. How many pixels wide is it?

    Pixels = inches times dpi, so the width is 5 x 100 = 500 pixels and the height is 3 x 100 = 300.
  2. 2. You raise dpi from 100 to 200 without changing figsize. What changes?

    DPI only changes the sampling resolution. Proportions and layout stay the same because they depend on figsize in inches.
  3. 3. Which layout engine is recommended for new code and handles colorbars well?

    Constrained layout recomputes spacing at every draw and accounts for colorbars, legends and suptitles.
  4. 4. Which call makes one x unit the same length as one y unit?

    set_aspect('equal') locks the aspect ratio so circles stay round.

Summary

  • figsize=(w, h) is in inches and sets the shape and the relative size of text; dpi sets sharpness.
  • Pixels = inches times dpi; the same figure at a higher dpi is sharper, not differently laid out.
  • Use layout="constrained" (or fig.tight_layout()) to stop clipped labels and overlaps.
  • ax.margins pads the data area; ax.set_aspect("equal") keeps circles round.
  • Set repeated sizes once through rcParams instead of in every call.

Next, go deeper into the most common chart in Line Plots in Depth.