39

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.

2
  • 1
    Your code does reduce the space between vertical subplots. Commented 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. Commented Mar 2, 2016 at 18:34

3 Answers 3

50

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

Sign up to request clarification or add additional context in comments.

4 Comments

how counterintuitive to have wspace instead of hspace!
I suppose hspace is height and wspace is width? But yes, a bit counterintuitive!
wspace and hspace force the subplots to get smaller.
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).
9

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

Comments

8

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 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.