Challenge - 5 Problems
Figure Size Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of figure size in inches
What will be the output shape of the figure in inches after running this code?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(6, 4)) print(fig.get_size_inches())
Attempts:
2 left
💡 Hint
The figsize parameter sets width and height in inches.
✗ Incorrect
The figsize argument in plt.figure sets the width and height in inches. The get_size_inches() method returns a numpy array with these values.
❓ data_output
intermediate1:30remaining
Number of pixels in saved figure
If you save a figure with figsize=(5, 3) and dpi=100, how many pixels wide and tall will the saved image be?
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(5, 3), dpi=100) width, height = fig.get_size_inches() * fig.dpi print(int(width), int(height))
Attempts:
2 left
💡 Hint
Pixels = inches * dpi.
✗ Incorrect
The figure size in inches multiplied by dpi gives the pixel dimensions of the saved image.
❓ visualization
advanced2:00remaining
Visual difference of figure sizes
Which option shows the correct code to create two side-by-side plots with different figure sizes for publication?
Attempts:
2 left
💡 Hint
Use one figure with subplots and set figsize once.
✗ Incorrect
Option D correctly creates one figure with two subplots side by side and sets the figure size once. Other options either create multiple figures or misuse subplot calls.
🧠 Conceptual
advanced1:30remaining
Effect of DPI on figure size and quality
How does increasing the dpi parameter affect the saved figure when figsize is fixed?
Attempts:
2 left
💡 Hint
Think about pixels per inch.
✗ Incorrect
DPI means dots per inch. Increasing dpi increases pixels per inch, so the image has more pixels and better quality but the physical size in inches stays the same.
🔧 Debug
expert2:00remaining
Why does this saved figure have unexpected size?
Given this code, why is the saved figure smaller than expected?
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 6))
plt.plot([1, 2, 3])
fig.savefig('plot.png', dpi=50)
Options:
Matplotlib
import matplotlib.pyplot as plt fig = plt.figure(figsize=(8, 6)) plt.plot([1, 2, 3]) fig.savefig('plot.png', dpi=50)
Attempts:
2 left
💡 Hint
Check how dpi affects saved image size.
✗ Incorrect
The dpi parameter in savefig controls the pixel density. Lower dpi means fewer pixels, so the saved image is smaller even if figsize is large.