0
0
Matplotlibdata~20 mins

Figure size and DPI in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Figure Size and DPI Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A400 300
B4 3
C100 75
D40 30
Attempts:
2 left
💡 Hint
Remember that figure size is in inches and DPI means dots per inch.
data_output
intermediate
2: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)
A(5, 2)
B(6, 4)
C(80, 60)
D(4, 6)
Attempts:
2 left
💡 Hint
The DataFrame shape is independent of figure size.
visualization
advanced
2: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')
Aimage1.png has higher resolution
BResolution depends only on figsize, not dpi
CBoth images have the same resolution
Dimage2.png has higher resolution
Attempts:
2 left
💡 Hint
DPI means dots per inch, so higher DPI means more pixels.
🧠 Conceptual
advanced
2:00remaining
Understanding figure size and DPI relationship
If you double the DPI but keep the figure size the same, what happens to the image?
AThe image becomes larger in inches
BThe image has more pixels and looks sharper
CThe image size and quality stay the same
DThe image becomes smaller in pixels
Attempts:
2 left
💡 Hint
Think about what DPI controls in an image.
🔧 Debug
expert
2: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')
AThe plot command is missing labels
BThe figsize is too large, causing blur
CThe dpi is too low, causing low pixel resolution
DThe savefig filename is incorrect
Attempts:
2 left
💡 Hint
Low DPI means fewer pixels in the image.