What is the difference between a figure's size and its DPI, and how do they interact when you save a raster image?

Quick Overview

Asks how a Matplotlib figure's size in inches and its DPI differ, and how they combine when you save a PNG or another raster image. Tests whether you can predict the pixel dimensions of an export and pick settings for print or screen without distorting the text.

What is the difference between a figure's size and its DPI, and how do they interact when you save a raster image?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is the difference between a Matplotlib figure's size and its DPI, and how do they interact when you save a raster image? ```hint Units Font sizes and line widths are set in points, a fixed fraction of an inch. Ask what happens to text when you change the inches and when you change the dots per inch. ``` ### What a Strong Answer Covers - What `figsize` controls and the unit it is measured in. - What DPI controls and what it leaves unchanged. - How the two combine into the pixel dimensions of a saved file, with a worked example. - How to choose the two settings for print and for screen. ### Follow-up Questions - You need a PNG exactly 1200 by 800 pixels for a web page, with readable text. How do you choose `figsize` and `dpi`? - Why can the saved PNG have different pixel dimensions from `figsize` times `dpi` when you pass `bbox_inches="tight"`? - What does `dpi` change when you save to PDF or SVG? - Why can a figure shown in a Jupyter notebook look different from the PNG you saved?

Overview: Asks how a Matplotlib figure's size in inches and its DPI differ, and how they combine when you save a PNG or another raster image. Tests whether you can predict the pixel dimensions of an export and pick settings for print or screen without distorting the text.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What is the difference between a Matplotlib figure's size and its DPI, and how do they interact when you save a raster image?

What a Strong Answer Covers Guidance

  • What figsize controls and the unit it is measured in.
  • What DPI controls and what it leaves unchanged.
  • How the two combine into the pixel dimensions of a saved file, with a worked example.
  • How to choose the two settings for print and for screen.

Follow-up Questions Guidance

  • You need a PNG exactly 1200 by 800 pixels for a web page, with readable text. How do you choose figsize and dpi ?
  • Why can the saved PNG have different pixel dimensions from figsize times dpi when you pass bbox_inches="tight" ?
  • What does dpi change when you save to PDF or SVG?
  • Why can a figure shown in a Jupyter notebook look different from the PNG you saved?