0
0
Matplotlibdata~20 mins

Unequal subplot sizes in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 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.