0
0
Matplotlibdata~20 mins

Subplot spacing adjustment in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subplot Spacing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the vertical space between subplots?

Consider the following code that creates two subplots stacked vertically. What is the vertical space between the two plots after running this code?

Matplotlib
import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 1)
fig.subplots_adjust(hspace=0.5)
plt.show()

space = fig.subplotpars.hspace
print(space)
A1.0
B0.0
C0.5
D0.1
Attempts:
2 left
💡 Hint

Check the hspace parameter in subplots_adjust.

data_output
intermediate
2:00remaining
How many subplots fit without overlap?

Given the code below, how many subplots will fit in the figure without overlapping when wspace=0.2 and hspace=0.2?

Matplotlib
import matplotlib.pyplot as plt

fig, axs = plt.subplots(3, 3)
fig.subplots_adjust(wspace=0.2, hspace=0.2)
plt.show()

rows = len(axs)
cols = len(axs[0])
print(rows * cols)
A6
B0
C3
D9
Attempts:
2 left
💡 Hint

Count the total number of subplots created by plt.subplots(3, 3).

visualization
advanced
2:00remaining
Which option produces the tightest subplot layout?

Which code snippet produces the tightest layout with minimal space between subplots?

Afig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1, wspace=0.05, hspace=0.05)
Bfig.subplots_adjust(left=0.2, right=0.8, top=0.8, bottom=0.2, wspace=0.5, hspace=0.5)
Cfig.subplots_adjust(left=0.0, right=1.0, top=1.0, bottom=0.0, wspace=1.0, hspace=1.0)
Dfig.subplots_adjust(left=0.3, right=0.7, top=0.7, bottom=0.3, wspace=0.3, hspace=0.3)
Attempts:
2 left
💡 Hint

Smaller wspace and hspace values mean less space between subplots.

🔧 Debug
advanced
2:00remaining
Why does this subplot spacing code raise an error?

What error does the following code raise and why?

import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
fig.subplots_adjust(wspace=-0.1, hspace=0.2)
plt.show()
ATypeError: wspace must be a float
BValueError: wspace must be >= 0
CNo error, plots display normally
DAttributeError: subplots_adjust has no attribute wspace
Attempts:
2 left
💡 Hint

Check the allowed range for wspace values.

🚀 Application
expert
3:00remaining
Adjust subplot spacing to avoid label overlap

You have a 2x2 grid of subplots with long x-axis labels that overlap. Which code snippet best adjusts spacing to avoid overlap while keeping the figure compact?

Matplotlib
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 2)
for ax in axs.flat:
    ax.set_xlabel('Long x-axis label example')
fig.subplots_adjust(PLACEHOLDER)
plt.show()
Aleft=0.1, right=0.9, top=0.9, bottom=0.3, wspace=0.3, hspace=0.4
Bleft=0.2, right=0.8, top=0.8, bottom=0.1, wspace=0.1, hspace=0.1
Cleft=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.05, hspace=0.05
Dleft=0.3, right=0.7, top=0.7, bottom=0.5, wspace=0.5, hspace=0.5
Attempts:
2 left
💡 Hint

Increasing bottom margin and vertical space helps avoid x-label overlap.