MatplotlibXBy @CodeWithMunnaX
Catalog
Track: Subplots & Layouts
Layouts10 mins

27. Figure Sizing, DPI & Aspect Ratios

Matplotlib defines figure dimensions physically in inches (`figsize=(width, height)`) and resolution in Dots Per Inch (`dpi`). Pixel resolution is: $\text{Pixels} = \text{Inches} \times \text{DPI}$.

Mental Model Intuition

Ordering a photograph print: specifying the physical print size (8x10 inches) and print resolution (300 DPI).

Core Concepts & Mechanics

The Sizing FormulaMath

Width in Pixels = `figsize[0] * dpi`. Height in Pixels = `figsize[1] * dpi`.

For web images, use `dpi=100`. For Retina/4K displays, use `dpi=200`. For academic print papers, use `dpi=300`.
plt.figure(figsize=(10, 6), dpi=150)

Common Beginner Mistakes & Pro Fixes

Avoid the top errors and bad plotting practices that trip up new Python developers

Troubleshooting

Quick Lesson Takeaways

  • `figsize=(w, h)` sets dimensions in inches.
  • `dpi` sets raster resolution (dots per inch).
  • Total pixels = inches * DPI.
Topic 27 • Python IDE
Python 3.12 • Matplotlib
Run Python code to inspect terminal output.

Real-World Datasets (1-Click Switcher)

Switch from abstract numbers to relatable Cricket, Cinema & Business charts

5 Curated Datasets

Hands-on Challenge: Set Figsize

+50 XP

Write Matplotlib code to solve the objective and see your live plot render

Objective Instructions:

Create a figure with `figsize=(8, 4)` and `dpi=100`, plot `[1, 2, 3]` and title it 'Custom Size'.

Required Keywords:✓ figsize✓ dpi
Challenge Code Editor
Challenge Output GraphLive Vector Preview
Write code on the left to render live challenge plot
End of Lesson Knowledge Check

Quick Mini Quiz: 2 Concept Questions

Test your understanding to unlock the lesson completion badge

Q1

Which Matplotlib function is used to create a standard continuous 2D line plot?

Q2

What must you call after specifying label='...' so that the legend actually appears on the canvas?

0 of 2 answered