Challenge - 5 Problems
Figure Size and DPI Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of figure size and DPI settings
What is the size in pixels of the figure created by this code?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4, 3), dpi=100) width, height = fig.get_size_inches() * fig.dpi print(int(width), int(height))
Attempts:
2 left
💡 Hint
Remember that figure size is in inches and DPI means dots per inch.
✗ Incorrect
The figure size is 4 inches by 3 inches. DPI is 100 dots per inch. So pixels = inches * DPI = 4*100=400 and 3*100=300.
❓ data_output
intermediate2:00remaining
DataFrame shape after resizing figure
Given this code, what is the shape of the DataFrame after plotting?
Matplotlib
import pandas as pd import matplotlib.pyplot as plt data = {'x': range(5), 'y': [10, 20, 15, 25, 30]} df = pd.DataFrame(data) fig, ax = plt.subplots(figsize=(6, 4), dpi=80) df.plot(x='x', y='y', ax=ax) print(df.shape)
Attempts:
2 left
💡 Hint
The DataFrame shape is independent of figure size.
✗ Incorrect
The DataFrame has 5 rows and 2 columns, so shape is (5, 2). Figure size does not change data shape.
❓ visualization
advanced2:00remaining
Effect of DPI on saved image resolution
Which saved image will have the highest resolution (most pixels)?
Matplotlib
import matplotlib.pyplot as plt fig1 = plt.figure(figsize=(5, 4), dpi=80) fig1.savefig('image1.png') fig2 = plt.figure(figsize=(5, 4), dpi=200) fig2.savefig('image2.png')
Attempts:
2 left
💡 Hint
DPI means dots per inch, so higher DPI means more pixels.
✗ Incorrect
Both images have the same size in inches, but image2.png has dpi=200, so it has more pixels and higher resolution.
🧠 Conceptual
advanced2:00remaining
Understanding figure size and DPI relationship
If you double the DPI but keep the figure size the same, what happens to the image?
Attempts:
2 left
💡 Hint
Think about what DPI controls in an image.
✗ Incorrect
Doubling DPI increases pixels per inch, so the image has more pixels and looks sharper at the same physical size.
🔧 Debug
expert2:00remaining
Why does this saved figure look blurry?
This code saves a figure but the image looks blurry. What is the main cause?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(3, 2), dpi=50) plt.plot([1, 2, 3], [4, 5, 6]) fig.savefig('blurry.png')
Attempts:
2 left
💡 Hint
Low DPI means fewer pixels in the image.
✗ Incorrect
DPI of 50 is low, so the saved image has few pixels and looks blurry when viewed at normal size.