Bird
Raised Fist0
Matplotlibdata~20 mins

Unequal subplot sizes in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Unequal Subplot Sizes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of unequal subplot sizes with gridspec
What is the output of this code snippet that creates two subplots with different widths using matplotlib's gridspec?
Matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(1, 2, width_ratios=[3, 1])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
ax1.plot([1, 2, 3], [1, 4, 9])
ax2.plot([1, 2, 3], [1, 2, 3])
plt.show()
print((ax1.get_position().width > ax2.get_position().width))
ARaises ValueError
BTrue
CRaises AttributeError
DFalse
Attempts:
2 left
💡 Hint
Check the width_ratios parameter and how it affects subplot widths.
❓ data_output
intermediate
1:30remaining
Number of axes created with unequal subplot sizes
How many axes objects are created by this code that uses GridSpec with unequal height ratios?
Matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(2, 1, height_ratios=[2, 1])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
plt.close(fig)
print(len(fig.axes))
A0
B1
C2
DRaises IndexError
Attempts:
2 left
💡 Hint
Count how many subplots are added to the figure.
🔧 Debug
advanced
2:00remaining
Identify the error in subplot size code
What error does this code raise when trying to create subplots with unequal sizes using gridspec?
Matplotlib
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure()
gs = GridSpec(1, 2, width_ratios=[2])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
plt.show()
AValueError: width_ratios must have length 2
BIndexError: index out of range
CTypeError: unsupported operand type(s)
DNo error, plots successfully
Attempts:
2 left
💡 Hint
Check the length of width_ratios compared to number of columns.
❓ visualization
advanced
2:30remaining
Visual difference in subplot heights
Which option produces a figure with two vertically stacked subplots where the top subplot is twice as tall as the bottom one?
A
gs = GridSpec(2, 1, height_ratios=[1, 2])
fig.add_subplot(gs[0])
fig.add_subplot(gs[1])
B
gs = GridSpec(1, 2, width_ratios=[2, 1])
fig.add_subplot(gs[0])
fig.add_subplot(gs[1])
C
gs = GridSpec(1, 2, width_ratios=[1, 2])
fig.add_subplot(gs[0])
fig.add_subplot(gs[1])
D
gs = GridSpec(2, 1, height_ratios=[2, 1])
fig.add_subplot(gs[0])
fig.add_subplot(gs[1])
Attempts:
2 left
💡 Hint
Height ratios control vertical size; width ratios control horizontal size.
🚀 Application
expert
3:00remaining
Create a complex layout with unequal subplot sizes
You want to create a figure with 3 subplots arranged in 2 rows and 2 columns. The top row has one subplot spanning both columns, and the bottom row has two subplots with widths ratio 3:1. Which code snippet achieves this layout?
A
gs = GridSpec(2, 2, width_ratios=[3, 1])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[1, 1])
B
gs = GridSpec(2, 2, width_ratios=[1, 3])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[1, 1])
C
gs = GridSpec(2, 2, height_ratios=[1, 2])
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[1, 1])
D
gs = GridSpec(2, 2)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, 0])
ax3 = fig.add_subplot(gs[1, 1])
Attempts:
2 left
💡 Hint
Use width_ratios to control bottom row widths and slice for top row spanning both columns.

Practice

(1/5)
1. What is the main advantage of using unequal subplot sizes in matplotlib?
easy
A. To automatically adjust colors of the plots
B. To make all charts the same size for uniformity
C. To highlight important charts by giving them more space
D. To reduce the total number of charts displayed

Solution

  1. Step 1: Understand subplot size purpose

    Unequal subplot sizes allow some plots to be larger, drawing more attention.
  2. Step 2: Compare options

    Only To highlight important charts by giving them more space mentions highlighting important charts by size, which is the main advantage.
  3. Final Answer:

    To highlight important charts by giving them more space -> Option C
  4. Quick Check:

    Unequal sizes highlight key charts [OK]
Hint: Unequal sizes = emphasize important plots [OK]
Common Mistakes:
  • Thinking all subplots must be equal size
  • Confusing size with color or number of plots
  • Assuming unequal sizes reduce charts
2. Which of the following is the correct way to create subplots with unequal sizes using gridspec_kw in plt.subplots()?
easy
A. plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]})
B. plt.subplots(2, 1, gridspec_kw={'height_ratios': [1, 2]})
C. plt.subplots(1, 2, gridspec_kw={'height_ratios': [2, 1]})
D. plt.subplots(2, 1, gridspec_kw={'width_ratios': [1, 2]})

Solution

  1. Step 1: Identify correct parameter for horizontal subplots

    For 1 row and 2 columns, use 'width_ratios' to set horizontal size ratios.
  2. Step 2: Match ratios to subplot layout

    plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]}) uses 1 row, 2 columns with width_ratios, which is correct syntax.
  3. Final Answer:

    plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]}) -> Option A
  4. Quick Check:

    Width ratios for columns = plt.subplots(1, 2, gridspec_kw={'width_ratios': [2, 1]}) [OK]
Hint: Use width_ratios for columns, height_ratios for rows [OK]
Common Mistakes:
  • Mixing width_ratios with rows instead of columns
  • Using height_ratios for horizontal layout
  • Wrong number of rows and columns in function
3. What will be the width ratio of the two subplots created by this code?
fig, axs = plt.subplots(1, 2, gridspec_kw={'width_ratios': [3, 1]})
medium
A. Both subplots have equal width
B. The subplots have random widths
C. The second subplot is 3 times wider than the first
D. The first subplot is 3 times wider than the second

Solution

  1. Step 1: Understand width_ratios parameter

    The list [3, 1] means the first subplot width is 3 units, second is 1 unit.
  2. Step 2: Interpret ratio meaning

    This means the first subplot is 3 times wider than the second subplot.
  3. Final Answer:

    The first subplot is 3 times wider than the second -> Option D
  4. Quick Check:

    Width ratio 3:1 means first is 3x wider [OK]
Hint: Ratio list directly shows relative sizes [OK]
Common Mistakes:
  • Assuming equal widths despite ratios
  • Reversing which subplot is wider
  • Ignoring the ratio values
4. Identify the error in this code that tries to create unequal subplot sizes:
fig, axs = plt.subplots(2, 1, gridspec_kw={'width_ratios': [1, 2]})
medium
A. Incorrect number of subplots specified
B. Using width_ratios for vertical subplots instead of height_ratios
C. Missing plt.show() to display plots
D. width_ratios list length does not match subplot count

Solution

  1. Step 1: Check subplot layout

    2 rows and 1 column means vertical stacking of subplots.
  2. Step 2: Verify correct ratio parameter

    For vertical layout, use 'height_ratios', not 'width_ratios'.
  3. Final Answer:

    Using width_ratios for vertical subplots instead of height_ratios -> Option B
  4. Quick Check:

    Vertical layout needs height_ratios [OK]
Hint: Use height_ratios for rows, width_ratios for columns [OK]
Common Mistakes:
  • Confusing width_ratios and height_ratios
  • Ignoring subplot grid shape
  • Assuming plt.show() affects subplot sizes
5. You want to create a figure with 3 subplots in one row where the middle subplot is twice as wide as the others. Which code correctly achieves this?
hard
A. plt.subplots(1, 3, gridspec_kw={'width_ratios': [1, 2, 1]})
B. plt.subplots(3, 1, gridspec_kw={'height_ratios': [1, 2, 1]})
C. plt.subplots(1, 3, gridspec_kw={'height_ratios': [1, 2, 1]})
D. plt.subplots(3, 1, gridspec_kw={'width_ratios': [1, 2, 1]})

Solution

  1. Step 1: Determine subplot layout

    One row and three columns means horizontal layout.
  2. Step 2: Choose correct ratio parameter

    For horizontal layout, use 'width_ratios' to set widths.
  3. Step 3: Set ratios to make middle subplot twice as wide

    Use [1, 2, 1] so middle subplot is twice the width of others.
  4. Final Answer:

    plt.subplots(1, 3, gridspec_kw={'width_ratios': [1, 2, 1]}) -> Option A
  5. Quick Check:

    Horizontal layout with width_ratios = plt.subplots(1, 3, gridspec_kw={'width_ratios': [1, 2, 1]}) [OK]
Hint: Middle subplot twice wide = width_ratios [1, 2, 1] [OK]
Common Mistakes:
  • Using height_ratios for horizontal layout
  • Setting wrong number of rows and columns
  • Reversing ratio order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard