Bird
Raised Fist0
Matplotlibdata~20 mins

DPI settings for resolution in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
DPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Effect of DPI on saved image size
What will be the size in pixels of the saved image when running this code?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(4, 3), dpi=100)
fig.savefig('test.png')
print(fig.get_size_inches() * fig.dpi)
A[100. 100.]
B[4. 3.]
C[400. 300.]
D[40. 30.]
Attempts:
2 left
💡 Hint
Multiply figure size in inches by dpi to get pixel dimensions.
data_output
intermediate
2:00remaining
DPI effect on figure pixel count
Given a figure with size (5, 2) inches and dpi=200, what is the total number of pixels in the saved image?
Matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5, 2), dpi=200)
width_px, height_px = fig.get_size_inches() * fig.dpi
print(int(width_px * height_px))
A20000
B400000
C2000
D10000
Attempts:
2 left
💡 Hint
Calculate width pixels * height pixels.
visualization
advanced
2:00remaining
Visual difference with DPI settings
Which saved image will appear sharper when viewed at the same physical size on screen?
Matplotlib
import matplotlib.pyplot as plt
fig1 = plt.figure(figsize=(3, 3), dpi=50)
plt.plot([1, 2, 3], [1, 4, 9])
fig1.savefig('low_dpi.png')
fig2 = plt.figure(figsize=(3, 3), dpi=200)
plt.plot([1, 2, 3], [1, 4, 9])
fig2.savefig('high_dpi.png')
AThe image saved with dpi=200 is sharper.
BBoth images have the same sharpness.
CThe image saved with dpi=50 is sharper.
DSharpness depends only on figure size, not dpi.
Attempts:
2 left
💡 Hint
Higher dpi means more pixels per inch, so more detail.
🔧 Debug
advanced
2:00remaining
Why does this saved figure look blurry?
This code saves a figure but the saved image looks blurry. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 4))
plt.plot([0, 1], [0, 1])
plt.savefig('blurry.png', dpi=50)
AThe savefig function needs a format argument to avoid blur.
BThe figure size is too small, causing blur.
CThe plot command is missing labels, causing blur.
DThe dpi is too low, causing low resolution.
Attempts:
2 left
💡 Hint
DPI controls image resolution; low dpi means fewer pixels.
🧠 Conceptual
expert
2:00remaining
Understanding DPI and figure size relationship
If you want to save a figure that is exactly 1200 pixels wide, which combination of figsize and dpi will achieve this?
Afigsize=(6, 4), dpi=200
Bfigsize=(10, 4), dpi=100
Cfigsize=(4, 3), dpi=250
Dfigsize=(10, 3), dpi=100
Attempts:
2 left
💡 Hint
Width in pixels = width in inches * dpi.

Practice

(1/5)
1. What does the dpi parameter control in matplotlib plots?
easy
A. The type of plot (line, bar, scatter)
B. The color scheme of the plot
C. The resolution or sharpness of the saved or displayed plot
D. The size of the plot in inches

Solution

  1. Step 1: Understand the role of DPI in images

    DPI stands for dots per inch and controls how many pixels are used per inch in an image, affecting sharpness.
  2. Step 2: Relate DPI to matplotlib plots

    In matplotlib, setting dpi changes the resolution of the saved or displayed plot, making it sharper or blurrier.
  3. Final Answer:

    The resolution or sharpness of the saved or displayed plot -> Option C
  4. Quick Check:

    DPI controls resolution = D [OK]
Hint: DPI means dots per inch, controlling image sharpness [OK]
Common Mistakes:
  • Confusing DPI with plot size
  • Thinking DPI changes plot colors
  • Assuming DPI changes plot type
2. Which of the following is the correct way to save a matplotlib plot with 300 DPI resolution?
easy
A. plt.savefig('plot.png', dpi='300')
B. plt.save('plot.png', dpi=300)
C. plt.savefig('plot.png', resolution=300)
D. plt.savefig('plot.png', dpi=300)

Solution

  1. Step 1: Recall the correct function to save plots

    The correct function to save a plot in matplotlib is plt.savefig().
  2. Step 2: Check the parameter for resolution

    The parameter to set resolution is dpi, so the correct syntax is plt.savefig('filename', dpi=300).
  3. Final Answer:

    plt.savefig('plot.png', dpi=300) -> Option D
  4. Quick Check:

    Use plt.savefig with dpi=300 = A [OK]
Hint: Use plt.savefig(filename, dpi=number) to set resolution [OK]
Common Mistakes:
  • Using plt.save instead of plt.savefig
  • Using 'resolution' instead of 'dpi'
  • Passing dpi as a string '300'
3. What will be the size in pixels of a saved plot with figsize=(4,3) inches and dpi=200?
medium
A. 4 x 3 pixels
B. 800 x 600 pixels
C. 200 x 150 pixels
D. 1000 x 750 pixels

Solution

  1. Step 1: Calculate width in pixels

    Width in pixels = width in inches * dpi = 4 * 200 = 800 pixels.
  2. Step 2: Calculate height in pixels

    Height in pixels = height in inches * dpi = 3 * 200 = 600 pixels.
  3. Final Answer:

    800 x 600 pixels -> Option B
  4. Quick Check:

    Pixels = inches * dpi = 800x600 [OK]
Hint: Multiply inches by dpi for pixel size [OK]
Common Mistakes:
  • Confusing dpi with inches
  • Multiplying dpi by 100 instead of inches
  • Using dpi as pixel count directly
4. Identify the error in this code snippet that tries to save a plot with 150 DPI:
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
plt.savefig('myplot.png', dpi='150')
medium
A. dpi value should be an integer, not a string
B. plt.plot syntax is incorrect
C. Filename must be .jpg not .png
D. Missing plt.show() before saving

Solution

  1. Step 1: Check the dpi parameter type

    The dpi parameter expects an integer number, but here it is passed as a string '150'.
  2. Step 2: Understand impact of wrong type

    Passing dpi as a string may cause a type error or unexpected behavior when saving the plot.
  3. Final Answer:

    dpi value should be an integer, not a string -> Option A
  4. Quick Check:

    dpi must be int, not string = A [OK]
Hint: dpi must be a number, not quoted text [OK]
Common Mistakes:
  • Passing dpi as string instead of int
  • Thinking plt.show() is needed before savefig
  • Assuming file extension affects dpi
5. You want to save a plot with a fixed pixel size of 1200x900 pixels. Which combination of figsize and dpi will achieve this?
hard
A. figsize=(6,4.5) and dpi=200
B. figsize=(12,10) and dpi=100
C. figsize=(10,8) and dpi=120
D. figsize=(8,7) and dpi=150

Solution

  1. Step 1: Understand pixel size formula

    Pixels = figsize (inches) * dpi. We want 1200 x 900 pixels.
  2. Step 2: Check each option's pixel size

    A: 12*100=1200, 10*100=1000 (wrong)
    B: 6*200=1200, 4.5*200=900 (correct)
    C: 10*120=1200, 8*120=960 (wrong)
    D: 8*150=1200, 7*150=1050 (wrong).
  3. Final Answer:

    figsize=(6,4.5) and dpi=200 -> Option A
  4. Quick Check:

    Pixels = inches * dpi = 1200x900 [OK]
Hint: Pixels = figsize * dpi; pick balanced values [OK]
Common Mistakes:
  • Choosing too large figsize with low dpi
  • Ignoring pixel size formula
  • Assuming dpi alone sets pixel size