0
0
Matplotlibdata~20 mins

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

Choose your learning style9 modes available
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.