Manipulation on vertical space in matplotlib subplots

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.


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


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

Tags:

Matplotlib