33

I want to reduce the verticalspacing between subplot. Surfing along the web I just have found how to reduce the horizontal spacing, something like

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=4, ncols=4)
fig.tight_layout() # Or equivalently,  "plt.tight_layout()"
fig.subplots_adjust(hspace=0.5)
plt.show()

The hspace thing is the one that manipulates such behaviour, but apparently there's no vspace.

EDIT:

This does not reduce the space between the y-axis, that is what I want to manipulate.

3
  • 1
    Your code does reduce the space between vertical subplots.
    – DavidG
    Mar 2, 2016 at 18:17
  • I mean, it does not reduce the space between y-axis in the subplots, that is what I want to do. Mar 2, 2016 at 18:34
  • Please read again and do not vote negatively, since the question was misunderstood! Mar 2, 2016 at 18:37

3 Answers 3

43

As you said in your question hspace reduces the vertical spacing between subplots. The equivalent for horizontal spacing between subplots is wspace. Below is an example:

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)


fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(nrows=2, ncols=2)
fig.tight_layout()
ax1.plot(x, y)
ax2.scatter(x, y)
ax3.scatter(x, y)
ax4.scatter(x, y)

fig.subplots_adjust(wspace=0.2)
plt.show()

Using a value for 1 for wspace gives enter image description here

Using 0.2 as the value of wspace gives

enter image description here

4
  • 2
    how counterintuitive to have wspace instead of hspace!
    – famargar
    Jan 25, 2018 at 12:38
  • 2
    I suppose hspace is height and wspace is width? But yes, a bit counterintuitive!
    – DavidG
    Jan 25, 2018 at 14:07
  • wspace and hspace force the subplots to get smaller. May 13, 2021 at 8:37
  • I've found out that fig.subplots_adjust(hspace=..., wspace=...) works with fig.tight_layout(), but silently fails when used with plt.subplots(..., tight_layout=True). (matplotlib 3.5.2). Sep 13, 2022 at 15:19
8

An alternative approach is to pass the gridspec_kw argument a dict with keys wspace / hspace:

Example

fig, axes = plt.subplots(nrows=2, ncols=2, gridspec_kw={'hspace': 0.2, 'wspace': 0.9})
plt.tight_layout()

for ax, color in zip(axes.ravel(), list('rgbk')):
    ax.scatter(np.arange(100), np.random.randn(100), color=color)

enter image description here

3

If I understood your question correctly, you want to reduce the vertical spacing, which is not what I have seen in all of the answers above.

If I am correct, you should reduce the hspace from 0.5 to 0.2, for instance. That's because hspace does not stand for horizontal spacing, it stands for height spacing, which is what you need.

1
  • Thank you for clarifying why you believe the existing answers aren’t relevant, and paraphrasing how you’re interpreting the question. That’s a good practice when answering old questions with established answers. May 25, 2020 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.