Consider the following code that creates two subplots stacked vertically. What is the vertical space between the two plots after running this code?
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)
Check the hspace parameter in subplots_adjust.
The hspace parameter controls the height (vertical) space between subplots. Setting hspace=0.5 means the vertical space is 0.5.
Given the code below, how many subplots will fit in the figure without overlapping when wspace=0.2 and hspace=0.2?
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)
Count the total number of subplots created by plt.subplots(3, 3).
The code creates a 3 by 3 grid of subplots, so there are 9 subplots total. The spacing parameters do not reduce the number of subplots.
Which code snippet produces the tightest layout with minimal space between subplots?
Smaller wspace and hspace values mean less space between subplots.
Option A uses the smallest wspace and hspace values (0.05), and the margins are reasonably tight, so it produces the tightest layout.
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()
Check the allowed range for wspace values.
The wspace parameter cannot be negative. Setting wspace=-0.1 raises a ValueError.
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?
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()
Increasing bottom margin and vertical space helps avoid x-label overlap.
Option A increases the bottom margin and vertical spacing to prevent label overlap while keeping the figure compact. Other options either have too small bottom margin or too large spacing.