Bird
Raised Fist0
Matplotlibdata~20 mins

Figure size for publication in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Figure Size Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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())
A[6. 4.]
B[4. 6.]
C[600 400]
D[0.6 0.4]
Attempts:
2 left
💡 Hint
The figsize parameter sets width and height in inches.
data_output
intermediate
1: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))
A50 30
B500 300
C5 3
D100 60
Attempts:
2 left
💡 Hint
Pixels = inches * dpi.
visualization
advanced
2: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?
A
fig, axs = plt.subplots(2, 1)
fig.set_size_inches(10, 4)
axs[0].plot([1, 2, 3])
axs[1].plot([3, 2, 1])
B
fig1 = plt.figure(figsize=(5, 4))
fig2 = plt.figure(figsize=(10, 8))
plt.plot([1, 2, 3])
C
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.plot([1, 2, 3])
plt.figure(figsize=(5, 4))
plt.subplot(122)
plt.plot([3, 2, 1])
D
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
axs[0].plot([1, 2, 3])
axs[1].plot([3, 2, 1])
Attempts:
2 left
💡 Hint
Use one figure with subplots and set figsize once.
🧠 Conceptual
advanced
1:30remaining
Effect of DPI on figure size and quality
How does increasing the dpi parameter affect the saved figure when figsize is fixed?
AThe saved image has higher pixel dimensions and better quality but same physical size in inches.
BThe saved image has larger physical size in inches but same pixel dimensions.
CThe saved image has lower pixel dimensions and worse quality.
DThe saved image size and quality do not change with dpi.
Attempts:
2 left
💡 Hint
Think about pixels per inch.
🔧 Debug
expert
2: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)
AThe plot command resets the figure size to default.
BThe figsize is ignored when dpi is set in savefig.
CThe dpi in savefig is lower than default, reducing pixel dimensions and making the image smaller.
DThe file format PNG does not support large figure sizes.
Attempts:
2 left
💡 Hint
Check how dpi affects saved image size.

Practice

(1/5)
1. What does the figsize parameter control in a matplotlib figure?
easy
A. The font size of the labels
B. The width and height of the figure in inches
C. The color of the figure background
D. The style of the plot lines

Solution

  1. Step 1: Understand the role of figsize

    The figsize parameter sets the size of the entire figure in inches, controlling width and height.
  2. Step 2: Differentiate from other parameters

    Other parameters like color or font size do not affect figure size but appearance details.
  3. Final Answer:

    The width and height of the figure in inches -> Option B
  4. Quick Check:

    Figure size = width and height in inches [OK]
Hint: Remember figsize sets width and height in inches [OK]
Common Mistakes:
  • Confusing figsize with color or font size
  • Thinking figsize controls plot line style
  • Assuming figsize is in pixels
2. Which of the following is the correct way to set a figure size of 8 inches wide and 4 inches tall in matplotlib?
easy
A. plt.figure(size=[8, 4])
B. plt.figure(size=(8, 4))
C. plt.figure(width=8, height=4)
D. plt.figure(figsize=(8, 4))

Solution

  1. Step 1: Recall correct parameter name and type

    The parameter to set figure size is figsize and it expects a tuple (width, height).
  2. Step 2: Check syntax correctness

    plt.figure(figsize=(8, 4)) uses figsize=(8, 4) which is correct syntax. Using size causes TypeError (unexpected keyword). Using width=8, height=4 also causes TypeError (no such parameters).
  3. Final Answer:

    plt.figure(figsize=(8, 4)) -> Option D
  4. Quick Check:

    Use figsize=(width, height) tuple [OK]
Hint: Use figsize=(width, height) tuple in plt.figure() [OK]
Common Mistakes:
  • Using 'size' instead of 'figsize'
  • Using separate width and height parameters
  • Forgetting parentheses around figsize values
3. What will be the size of the figure in inches after running this code?
import matplotlib.pyplot as plt
plt.figure(figsize=(6, 3))
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
medium
A. 6 inches wide and 3 inches tall
B. 3 inches wide and 6 inches tall
C. Default size (usually 6.4 x 4.8 inches)
D. Cannot determine without dpi

Solution

  1. Step 1: Identify figsize parameter values

    The code sets figsize=(6, 3), which means width=6 inches and height=3 inches.
  2. Step 2: Understand effect on figure size

    This directly sets the figure size regardless of dpi, so the figure will be 6 inches wide and 3 inches tall.
  3. Final Answer:

    6 inches wide and 3 inches tall -> Option A
  4. Quick Check:

    figsize=(6, 3) means width=6, height=3 inches [OK]
Hint: figsize=(width, height) sets exact figure size in inches [OK]
Common Mistakes:
  • Swapping width and height values
  • Thinking dpi affects figsize dimensions
  • Assuming default size if figsize is set
4. Identify the error in this code that tries to set figure size:
import matplotlib.pyplot as plt
plt.figure(figsize=8, 4)
plt.plot([1, 2], [3, 4])
plt.show()
medium
A. No error, code runs fine
B. plt.plot syntax is incorrect
C. figsize should be a tuple, not two separate arguments
D. plt.show() is missing parentheses

Solution

  1. Step 1: Check figsize parameter usage

    The code uses figsize=8, 4 which passes two separate arguments instead of a single tuple.
  2. Step 2: Understand correct figsize syntax

    Correct syntax requires a tuple: figsize=(8, 4). Without parentheses, it causes a TypeError.
  3. Final Answer:

    figsize should be a tuple, not two separate arguments -> Option C
  4. Quick Check:

    figsize=(width, height) needs parentheses [OK]
Hint: Always use parentheses for figsize tuple [OK]
Common Mistakes:
  • Passing figsize values without parentheses
  • Confusing plt.plot syntax errors
  • Forgetting plt.show() parentheses (not the case here)
5. You want to create a publication-ready plot with a width of 7 inches and height of 5 inches. You also want to save it as a PNG file with 300 dpi resolution. Which code snippet correctly sets the figure size and saves the plot?
hard
A. plt.figure(figsize=(7, 5)) plt.plot(data) plt.savefig('plot.png', dpi=300)
B. plt.figure(size=(7, 5)) plt.plot(data) plt.savefig('plot.png', dpi=300)
C. plt.figure(figsize=[7, 5]) plt.plot(data) plt.savefig('plot.png')
D. plt.figure(figsize=(7, 5)) plt.plot(data) plt.save('plot.png', dpi=300)

Solution

  1. Step 1: Set figure size correctly

    Use figsize=(7, 5) tuple in plt.figure() to set width and height in inches.
  2. Step 2: Save figure with correct dpi and function

    Use plt.savefig('plot.png', dpi=300) to save with 300 dpi resolution. plt.figure(size=(7, 5)) plt.plot(data) plt.savefig('plot.png', dpi=300) uses wrong parameter size. plt.figure(figsize=[7, 5]) plt.plot(data) plt.savefig('plot.png') misses dpi. plt.figure(figsize=(7, 5)) plt.plot(data) plt.save('plot.png', dpi=300) uses wrong function plt.save.
  3. Final Answer:

    plt.figure(figsize=(7, 5)) plt.plot(data) plt.savefig('plot.png', dpi=300) -> Option A
  4. Quick Check:

    figsize tuple + savefig with dpi=300 [OK]
Hint: Use figsize tuple and savefig with dpi for publication [OK]
Common Mistakes:
  • Using 'size' instead of 'figsize'
  • Forgetting dpi in savefig for quality
  • Using plt.save instead of plt.savefig