What does 'figure size' control in a matplotlib plot?
Figure size controls the width and height of the plot in inches. It determines how big the plot will appear on the screen or in a saved image.
Click to reveal answer
beginner
What is DPI in matplotlib and why is it important?
DPI stands for Dots Per Inch. It controls the resolution of the figure, meaning how many dots (pixels) are used per inch. Higher DPI means sharper images.
Click to reveal answer
beginner
How do you set the figure size to 8 inches wide and 6 inches tall in matplotlib?
Use plt.figure(figsize=(8, 6)) to set the figure size to 8 inches wide and 6 inches tall.
Click to reveal answer
intermediate
How does increasing DPI affect the saved image size and quality?
Increasing DPI increases the number of pixels per inch, making the image sharper and larger in pixel dimensions. The file size usually becomes bigger too.
Click to reveal answer
intermediate
What happens if you set a large figure size but low DPI?
The plot will be physically large but may look blurry or pixelated because the resolution (DPI) is low.
Click to reveal answer
In matplotlib, what does figsize=(10, 5) mean?
AThe figure has 10 dots per inch horizontally and 5 vertically
BThe figure is 10 inches wide and 5 inches tall
CThe figure is 10 pixels wide and 5 pixels tall
DThe figure will be saved with 10 DPI
✗ Incorrect
figsize sets the width and height in inches, so (10, 5) means 10 inches wide and 5 inches tall.
What does a higher DPI value do to a matplotlib figure?
AIncreases the resolution and sharpness of the figure
BMakes the figure physically bigger on screen
CDecreases the number of pixels in the figure
DChanges the color of the figure
✗ Incorrect
Higher DPI means more dots per inch, so the figure is sharper and has higher resolution.
Which code sets a figure size of 6x4 inches with 150 DPI?
Aplt.figure(figsize=(6,4), dpi=150)
Bplt.figure(size=(6,4), dpi=150)
Cplt.figure(figsize=(150,6), dpi=4)
Dplt.figure(dpi=(6,4), figsize=150)
✗ Incorrect
The correct syntax is figsize=(width, height) in inches and dpi=resolution.
If you want a small image file but still clear, what should you do?
AUse large figure size and low DPI
BUse small figure size and low DPI
CUse small figure size and high DPI
DUse large figure size and high DPI
✗ Incorrect
Small figure size reduces pixel count, and high DPI keeps the image sharp.
What unit is used for figure size in matplotlib?
APoints
BCentimeters
CPixels
DInches
✗ Incorrect
Matplotlib uses inches for figure size.
Explain how figure size and DPI work together to affect the appearance and quality of a matplotlib plot.
Think about how size and resolution affect a photo's clarity.
You got /4 concepts.
Describe how you would create a matplotlib figure that is 10 inches wide, 5 inches tall, and has a resolution of 200 DPI.
Remember figsize is a tuple of inches, dpi is dots per inch.
You got /4 concepts.
Practice
(1/5)
1. What does the figsize parameter control in a matplotlib figure?
easy
A. The physical size of the figure in inches
B. The color of the figure background
C. The resolution of the figure in dots per inch
D. The font size of the plot labels
Solution
Step 1: Understand the role of figsize
The figsize parameter sets the width and height of the figure in inches.
Step 2: Differentiate from DPI
DPI controls resolution, not size. Color and font size are unrelated to figsize.
Final Answer:
The physical size of the figure in inches -> Option A
2. Which of the following is the correct way to create a matplotlib figure with size 8 inches wide and 4 inches tall?
easy
A. plt.figure(dpi=(8, 4))
B. plt.figure(size=(8, 4))
C. plt.figure(figsize=(8, 4))
D. plt.figure(shape=(8, 4))
Solution
Step 1: Recall correct parameter name
The parameter to set figure size is figsize, which takes a tuple (width, height).
Step 2: Check syntax correctness
Only plt.figure(figsize=(8, 4)) uses figsize correctly. Others use invalid parameter names.
Final Answer:
plt.figure(figsize=(8, 4)) -> Option C
Quick Check:
Use figsize=(width, height) [OK]
Hint: Use figsize=(width, height) tuple in plt.figure() [OK]
Common Mistakes:
Using wrong parameter names like size or shape
Passing DPI values instead of size
Not using a tuple for figsize
3. What will be the DPI of the figure created by this code?
fig = plt.figure(figsize=(6, 3), dpi=150)
medium
A. 100
B. 150
C. 6
D. 3
Solution
Step 1: Identify the DPI parameter value
The code explicitly sets dpi=150 when creating the figure.
Step 2: Understand DPI meaning
DPI means dots per inch, so the figure resolution is 150 dots per inch.
Final Answer:
150 -> Option B
Quick Check:
DPI parameter sets resolution [OK]
Hint: DPI is set by dpi= value in plt.figure() [OK]
Common Mistakes:
Confusing DPI with figure size
Choosing width or height as DPI
Assuming default DPI without checking code
4. Identify the error in this code snippet:
plt.figure(figsize=8, dpi=100)
medium
A. No error, code is correct
B. dpi must be a tuple, not an integer
C. plt.figure does not accept figsize parameter
D. figsize must be a tuple, not a single integer
Solution
Step 1: Check the type of figsize argument
figsize expects a tuple (width, height), but 8 is a single integer.
Step 2: Verify dpi argument
dpi accepts an integer, so dpi=100 is correct.
Final Answer:
figsize must be a tuple, not a single integer -> Option D
Quick Check:
figsize = (width, height) tuple [OK]
Hint: Always pass figsize as (width, height) tuple [OK]
Common Mistakes:
Passing single int instead of tuple to figsize
Thinking dpi needs a tuple
Assuming figsize can be a single number
5. You want to save a plot image that is 10 inches wide and 5 inches tall with a resolution of 300 DPI. Which code snippet correctly creates and saves this figure?
hard
A. fig = plt.figure(figsize=(10, 5))
plt.plot([1, 2, 3])
fig.savefig('plot.png', dpi=300)
B. fig = plt.figure(figsize=(10, 5), dpi=100)
plt.plot([1, 2, 3])
fig.savefig('plot.png')
C. fig = plt.figure(dpi=300)
plt.plot([1, 2, 3])
fig.savefig('plot.png', figsize=(10, 5))
D. fig = plt.figure(figsize=10, dpi=300)
plt.plot([1, 2, 3])
fig.savefig('plot.png')
Solution
Step 1: Understand figure creation and saving
Setting figsize in plt.figure() controls size on screen, but saving with specific DPI requires passing dpi to savefig().
Step 2: Analyze each option
fig = plt.figure(figsize=(10, 5), dpi=100)
plt.plot([1, 2, 3])
fig.savefig('plot.png') sets DPI in figure but saving uses default DPI, so output may differ. fig = plt.figure(figsize=(10, 5))
plt.plot([1, 2, 3])
fig.savefig('plot.png', dpi=300) sets size in figure and DPI in savefig, ensuring correct size and resolution. fig = plt.figure(dpi=300)
plt.plot([1, 2, 3])
fig.savefig('plot.png', figsize=(10, 5)) wrongly passes figsize to savefig, which is invalid. fig = plt.figure(figsize=10, dpi=300)
plt.plot([1, 2, 3])
fig.savefig('plot.png') passes single int to figsize, causing error.