Bird
Raised Fist0
Matplotlibdata~20 mins

Subplot spacing adjustment in Matplotlib - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What does the plt.subplots_adjust() function do in matplotlib?
easy
A. It changes the space between subplots in a figure.
B. It creates new subplots in a figure.
C. It saves the figure to a file.
D. It changes the color of the plot lines.

Solution

  1. Step 1: Understand the purpose of plt.subplots_adjust()

    This function is used to control the spacing between subplots in a matplotlib figure.
  2. Step 2: Compare options

    Only It changes the space between subplots in a figure. correctly describes adjusting space between subplots. Other options describe different functions.
  3. Final Answer:

    It changes the space between subplots in a figure. -> Option A
  4. Quick Check:

    Subplot spacing adjustment = plt.subplots_adjust() [OK]
Hint: Remember: adjust spacing, not create or save plots [OK]
Common Mistakes:
  • Confusing subplot creation with spacing adjustment
  • Thinking it saves the figure
  • Mixing it with styling functions
2. Which of the following is the correct syntax to increase horizontal space between subplots using plt.subplots_adjust()?
easy
A. plt.subplots_adjust(hspace=0.5)
B. plt.subplots_adjust(space=0.5)
C. plt.subplots_adjust(wspace=0.5)
D. plt.subplots_adjust(widthspace=0.5)

Solution

  1. Step 1: Identify parameter for horizontal spacing

    The parameter wspace controls horizontal space between subplots.
  2. Step 2: Check syntax correctness

    plt.subplots_adjust(wspace=0.5) uses wspace=0.5, which is correct. Other options use incorrect or non-existent parameters.
  3. Final Answer:

    plt.subplots_adjust(wspace=0.5) -> Option C
  4. Quick Check:

    wspace adjusts horizontal space [OK]
Hint: Use wspace for width (horizontal), hspace for height (vertical) [OK]
Common Mistakes:
  • Using hspace instead of wspace for horizontal spacing
  • Using wrong parameter names like space or widthspace
  • Syntax errors in function call
3. What will be the effect of this code on subplot spacing?
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
plt.subplots_adjust(wspace=1.0)
plt.show()
medium
A. Subplots will have increased horizontal space between them.
B. Subplots will be very close horizontally with almost no space.
C. Subplots will overlap each other.
D. Subplots will have increased vertical space between them.

Solution

  1. Step 1: Understand wspace=1.0 effect

    Setting wspace to 1.0 increases horizontal space between subplots significantly.
  2. Step 2: Analyze code behavior

    Since there are 2 subplots side by side, they will be spaced apart horizontally, not vertically.
  3. Final Answer:

    Subplots will have increased horizontal space between them. -> Option A
  4. Quick Check:

    wspace=1.0 means more horizontal space [OK]
Hint: wspace controls horizontal gap size between subplots [OK]
Common Mistakes:
  • Confusing wspace with hspace
  • Expecting subplots to overlap
  • Thinking spacing affects vertical gap
4. Identify the error in this code that tries to adjust subplot spacing:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2, 1)
plt.subplots_adjust(wspace=0.3, hspace=0.5)
plt.show()
medium
A. Using hspace with vertical subplots is invalid.
B. Using wspace when subplots are stacked vertically.
C. Incorrect number of subplots created.
D. Missing plt.show() call.

Solution

  1. Step 1: Analyze subplot layout

    The code creates 2 subplots stacked vertically (2 rows, 1 column).
  2. Step 2: Check spacing parameters

    wspace controls horizontal spacing, which is irrelevant here because subplots are vertical. Only hspace affects vertical spacing.
  3. Final Answer:

    Using wspace when subplots are stacked vertically. -> Option B
  4. Quick Check:

    wspace for horizontal, not vertical spacing [OK]
Hint: Match wspace to horizontal, hspace to vertical layout [OK]
Common Mistakes:
  • Using wspace for vertical subplot spacing
  • Ignoring subplot grid shape
  • Assuming hspace is invalid for vertical layout
5. You have a 2x2 grid of subplots with overlapping x-axis labels. Which plt.subplots_adjust() call best reduces overlap by increasing vertical space and adding some horizontal space?
hard
A. plt.subplots_adjust(wspace=1.5, hspace=0.0)
B. plt.subplots_adjust(wspace=0.0, hspace=0.0)
C. plt.subplots_adjust(wspace=0.0, hspace=1.5)
D. plt.subplots_adjust(wspace=0.4, hspace=0.6)

Solution

  1. Step 1: Understand the problem of overlapping x-axis labels

    Overlapping x-axis labels usually happen vertically between rows, so increasing vertical space (hspace) helps.
  2. Step 2: Consider horizontal space

    Adding some horizontal space (wspace) also improves readability between columns.
  3. Step 3: Evaluate options

    plt.subplots_adjust(wspace=0.4, hspace=0.6) increases both wspace and hspace moderately, which best reduces overlap without wasting space.
  4. Final Answer:

    plt.subplots_adjust(wspace=0.4, hspace=0.6) -> Option D
  5. Quick Check:

    Increase hspace and wspace moderately to reduce overlap [OK]
Hint: Increase hspace for vertical gaps, wspace for horizontal gaps [OK]
Common Mistakes:
  • Setting spacing to zero causing overlap
  • Increasing only one spacing dimension
  • Using too large spacing causing wasted space
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