Manipulation on vertical space in Matplotlib subplots

To manipulate vertical space in Matplotlib subplots, we can use the hspace parameter in the subplots_adjust() method. This allows us to control the spacing between subplot rows.

Understanding hspace Parameter

The hspace parameter controls the height of padding between subplots as a fraction of the average subplot height. Values greater than 1.0 create more space, while values less than 1.0 reduce space.

Basic Example with Vertical Space Adjustment

Let's create a 2×2 subplot layout and adjust the vertical spacing ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

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

# Create subplots
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)

# Plot data in each subplot
ax1.plot(x, y)
ax1.set_title('Subplot 1')

ax2.plot(x, np.cos(x))
ax2.set_title('Subplot 2')

ax3.plot(x, np.tan(x))
ax3.set_title('Subplot 3')

ax4.plot(x, y * 2)
ax4.set_title('Subplot 4')

# Adjust vertical space
fig.subplots_adjust(hspace=1)

plt.show()

Comparing Different hspace Values

Here's how different hspace values affect vertical spacing ?

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)

# Create figure with multiple examples
fig = plt.figure(figsize=(12, 8))

# Example 1: hspace = 0.2 (tight spacing)
plt.subplot(1, 3, 1)
fig1, axes1 = plt.subplots(3, 1, figsize=(4, 6))
for i, ax in enumerate(axes1):
    ax.plot(x, np.sin(x + i))
    ax.set_title(f'Plot {i+1}')
fig1.subplots_adjust(hspace=0.2)
plt.title('hspace=0.2')

# Example 2: hspace = 0.5 (medium spacing)  
fig2, axes2 = plt.subplots(3, 1, figsize=(4, 6))
for i, ax in enumerate(axes2):
    ax.plot(x, np.cos(x + i))
    ax.set_title(f'Plot {i+1}')
fig2.subplots_adjust(hspace=0.5)

# Example 3: hspace = 1.0 (large spacing)
fig3, axes3 = plt.subplots(3, 1, figsize=(4, 6))  
for i, ax in enumerate(axes3):
    ax.plot(x, np.tan(x + i))
    ax.set_title(f'Plot {i+1}')
fig3.subplots_adjust(hspace=1.0)

plt.show()

Key Parameters

Parameter Description Default Value
hspace Vertical spacing between subplot rows 0.2
wspace Horizontal spacing between subplot columns 0.2
top Top margin of the subplot area 0.9
bottom Bottom margin of the subplot area 0.1

Complete Example with Multiple Adjustments

import numpy as np
import matplotlib.pyplot as plt

# Create data
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = np.sin(2 * x)

# Create subplots
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 8))

# Plot data
ax1.plot(x, y1, 'r-')
ax1.set_title('sin(x)')

ax2.plot(x, y2, 'g-')
ax2.set_title('cos(x)')

ax3.plot(x, y3, 'b-')
ax3.set_title('tan(x)')
ax3.set_ylim(-5, 5)

ax4.plot(x, y4, 'm-')
ax4.set_title('sin(2x)')

# Adjust both vertical and horizontal spacing
fig.subplots_adjust(hspace=0.8, wspace=0.4, top=0.9, bottom=0.1)

plt.show()

Conclusion

Use fig.subplots_adjust(hspace=value) to control vertical spacing between subplot rows. Values around 0.2-0.5 work well for most cases, while larger values like 1.0 provide generous spacing for complex plots with titles and labels.

Updated on: 2026-03-25T21:39:07+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements