Reputation: 3253
I have a library function which generates a figure with a 5x5 grid of subplots, using pyplot. In some instances, I'd like to remove some of the columns or lines to give more space to whichever subplots are more rlevant in some particular scenario.
I've found that I can delete subplots after the fact, using fig.delaxes(ax)
.
However, that just leaves an empty space where the subplot was, even if I delete a whole row or column of subplots this way, change the spacings between subplots, redraw or use fig.tight_layout()
.
Is there a method to get Matplotlib to close those gaps? I could probably edit the code which generates the figures to skip the rows/columns in question, but I'd like to avoid that if possible.
Upvotes: 1
Views: 1140
Reputation: 5913
Sure, you can set the height_ratios after the fact:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(3, 5, constrained_layout=True)
for i in range(5):
axs[1, i].set_xticks([])
axs[1, i].set_yticks([])
gs = axs[0, 0].get_subplotspec().get_gridspec()
gs.set_height_ratios([1, 0.0001, 1])
plt.tight_layout()
for ax in axs[1, :]:
fig.delaxes(ax)
... which isn't perfect, but pretty close...
Upvotes: 2