Bird
Raised Fist0
Matplotlibdata~10 mins

Figure size and DPI in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Figure size and DPI
Create Figure with size (width, height)
↓
Set DPI (dots per inch) for resolution
↓
Plot data on figure canvas
↓
Render figure with specified size and DPI
↓
Display or save the figure image
This flow shows how matplotlib creates a figure with a set size and resolution (DPI), then plots data and renders the image accordingly.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 3), dpi=100)
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
Creates a 4x3 inch figure with 100 DPI, plots a simple line, and displays the figure.
Execution Table
StepActionParameter/ValueEffect on FigureOutput
1Create figurefigsize=(4,3)Figure size set to 4 inches wide, 3 inches tallFigure object created
2Set DPIdpi=100Resolution set to 100 dots per inchFigure resolution defined
3Plot datax=[1,2,3], y=[4,5,6]Line plotted on figure canvasLine plot ready
4Render figuresize=4x3 inches, dpi=100Canvas pixels = 400x300 (4*100 by 3*100)Figure rendered with correct size and resolution
5Display figureplt.show()Figure window opens showing plotVisual output displayed
6ExitEnd of codeNo further actionExecution stops
💡 Code finishes after displaying the figure window
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
figsizeNone(4, 3)(4, 3)(4, 3)(4, 3)(4, 3)
dpiNoneNone100100100100
figure_pixelsNoneNoneNoneNone(400, 300)(400, 300)
plot_dataNoneNoneNonex=[1,2,3], y=[4,5,6]x=[1,2,3], y=[4,5,6]x=[1,2,3], y=[4,5,6]
Key Moments - 2 Insights
Why does changing DPI affect the pixel size but not the figure size in inches?
Because DPI means dots per inch, it controls how many pixels fit in each inch. The figure size in inches stays the same, but higher DPI means more pixels, so higher resolution. See execution_table step 4 where 4 inches * 100 DPI = 400 pixels.
What happens if we set figsize but not DPI?
Matplotlib uses a default DPI (usually 100). So the figure size in inches is set, but pixel size depends on default DPI. This is like step 1 without step 2 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the pixel size of the figure canvas?
A400x300 pixels
B4x3 pixels
C100x100 pixels
D40x30 pixels
💡 Hint
Check the 'Effect on Figure' column at step 4 in the execution_table.
According to variable_tracker, what is the DPI value after step 2?
ANone
B50
C100
D300
💡 Hint
Look at the 'dpi' row under 'After Step 2' in variable_tracker.
If we change figsize to (8, 6) but keep dpi=100, what will be the pixel size of the figure?
A400x300 pixels
B800x600 pixels
C100x100 pixels
D1600x1200 pixels
💡 Hint
Multiply width and height in inches by DPI as shown in execution_table step 4.
Concept Snapshot
matplotlib.figure(figsize=(width, height), dpi=resolution)
- figsize sets figure size in inches
- dpi sets dots per inch (pixel density)
- Pixel size = figsize * dpi
- Higher dpi means sharper images
- Use plt.show() to display the figure
Full Transcript
This visual execution trace shows how matplotlib creates a figure with a specific size and resolution. First, the figure is created with a size in inches (figsize). Then, the DPI (dots per inch) sets how many pixels fit in each inch, affecting resolution. Data is plotted on the figure canvas. The figure is rendered with pixel dimensions calculated by multiplying figsize by dpi. Finally, the figure is displayed. Variables like figsize, dpi, and pixel size change step by step. Key moments clarify why DPI affects pixels but not inches, and what happens if DPI is not set. The quiz tests understanding of pixel size, DPI value, and effects of changing figsize.

Practice

(1/5)
1. What does the figsize parameter control in a matplotlib figure?
easy
A. The physical size of the figure in inches
B. The color of the figure background
C. The resolution of the figure in dots per inch
D. The font size of the plot labels

Solution

  1. Step 1: Understand the role of figsize

    The figsize parameter sets the width and height of the figure in inches.
  2. Step 2: Differentiate from DPI

    DPI controls resolution, not size. Color and font size are unrelated to figsize.
  3. Final Answer:

    The physical size of the figure in inches -> Option A
  4. Quick Check:

    Figure size = physical dimensions [OK]
Hint: Remember: figsize = inches size, DPI = sharpness [OK]
Common Mistakes:
  • Confusing DPI with figure size
  • Thinking figsize controls resolution
  • Mixing up color or font size with figsize
2. Which of the following is the correct way to create a matplotlib figure with size 8 inches wide and 4 inches tall?
easy
A. plt.figure(dpi=(8, 4))
B. plt.figure(size=(8, 4))
C. plt.figure(figsize=(8, 4))
D. plt.figure(shape=(8, 4))

Solution

  1. Step 1: Recall correct parameter name

    The parameter to set figure size is figsize, which takes a tuple (width, height).
  2. Step 2: Check syntax correctness

    Only plt.figure(figsize=(8, 4)) uses figsize correctly. Others use invalid parameter names.
  3. Final Answer:

    plt.figure(figsize=(8, 4)) -> Option C
  4. Quick Check:

    Use figsize=(width, height) [OK]
Hint: Use figsize=(width, height) tuple in plt.figure() [OK]
Common Mistakes:
  • Using wrong parameter names like size or shape
  • Passing DPI values instead of size
  • Not using a tuple for figsize
3. What will be the DPI of the figure created by this code?
fig = plt.figure(figsize=(6, 3), dpi=150)
medium
A. 100
B. 150
C. 6
D. 3

Solution

  1. Step 1: Identify the DPI parameter value

    The code explicitly sets dpi=150 when creating the figure.
  2. Step 2: Understand DPI meaning

    DPI means dots per inch, so the figure resolution is 150 dots per inch.
  3. Final Answer:

    150 -> Option B
  4. Quick Check:

    DPI parameter sets resolution [OK]
Hint: DPI is set by dpi= value in plt.figure() [OK]
Common Mistakes:
  • Confusing DPI with figure size
  • Choosing width or height as DPI
  • Assuming default DPI without checking code
4. Identify the error in this code snippet:
plt.figure(figsize=8, dpi=100)
medium
A. No error, code is correct
B. dpi must be a tuple, not an integer
C. plt.figure does not accept figsize parameter
D. figsize must be a tuple, not a single integer

Solution

  1. Step 1: Check the type of figsize argument

    figsize expects a tuple (width, height), but 8 is a single integer.
  2. Step 2: Verify dpi argument

    dpi accepts an integer, so dpi=100 is correct.
  3. Final Answer:

    figsize must be a tuple, not a single integer -> Option D
  4. Quick Check:

    figsize = (width, height) tuple [OK]
Hint: Always pass figsize as (width, height) tuple [OK]
Common Mistakes:
  • Passing single int instead of tuple to figsize
  • Thinking dpi needs a tuple
  • Assuming figsize can be a single number
5. You want to save a plot image that is 10 inches wide and 5 inches tall with a resolution of 300 DPI. Which code snippet correctly creates and saves this figure?
hard
A. fig = plt.figure(figsize=(10, 5)) plt.plot([1, 2, 3]) fig.savefig('plot.png', dpi=300)
B. fig = plt.figure(figsize=(10, 5), dpi=100) plt.plot([1, 2, 3]) fig.savefig('plot.png')
C. fig = plt.figure(dpi=300) plt.plot([1, 2, 3]) fig.savefig('plot.png', figsize=(10, 5))
D. fig = plt.figure(figsize=10, dpi=300) plt.plot([1, 2, 3]) fig.savefig('plot.png')

Solution

  1. Step 1: Understand figure creation and saving

    Setting figsize in plt.figure() controls size on screen, but saving with specific DPI requires passing dpi to savefig().
  2. Step 2: Analyze each option

    fig = plt.figure(figsize=(10, 5), dpi=100) plt.plot([1, 2, 3]) fig.savefig('plot.png') sets DPI in figure but saving uses default DPI, so output may differ. fig = plt.figure(figsize=(10, 5)) plt.plot([1, 2, 3]) fig.savefig('plot.png', dpi=300) sets size in figure and DPI in savefig, ensuring correct size and resolution. fig = plt.figure(dpi=300) plt.plot([1, 2, 3]) fig.savefig('plot.png', figsize=(10, 5)) wrongly passes figsize to savefig, which is invalid. fig = plt.figure(figsize=10, dpi=300) plt.plot([1, 2, 3]) fig.savefig('plot.png') passes single int to figsize, causing error.
  3. Final Answer:

    fig = plt.figure(figsize=(10, 5)) plt.plot([1, 2, 3]) fig.savefig('plot.png', dpi=300) -> Option A
  4. Quick Check:

    Set size in figure, DPI in savefig for correct output [OK]
Hint: Set figsize in figure, dpi in savefig to control size and resolution [OK]
Common Mistakes:
  • Setting dpi only in figure but not in savefig
  • Passing figsize to savefig instead of figure
  • Using single int instead of tuple for figsize
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard