Challenge - 5 Problems
DPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Multiply figure size in inches by dpi to get pixel dimensions.
✗ Incorrect
The figure size is 4 inches by 3 inches. The dpi is 100 dots per inch. Multiplying 4*100 and 3*100 gives 400 by 300 pixels.
❓ data_output
intermediate2: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))
Attempts:
2 left
💡 Hint
Calculate width pixels * height pixels.
✗ Incorrect
Width in pixels = 5 * 200 = 1000, height in pixels = 2 * 200 = 400. Total pixels = 1000 * 400 = 400,000.
❓ visualization
advanced2: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')
Attempts:
2 left
💡 Hint
Higher dpi means more pixels per inch, so more detail.
✗ Incorrect
Higher dpi means more pixels per inch, so the image has more detail and appears sharper when viewed at the same size.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
DPI controls image resolution; low dpi means fewer pixels.
✗ Incorrect
Low dpi means fewer pixels per inch, so the image looks blurry when viewed at normal size.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Width in pixels = width in inches * dpi.
✗ Incorrect
To get 1200 pixels width, multiply width in inches by dpi. For option A: 6 * 200 = 1200 pixels.
Other options do not produce exactly 1200 pixels width.