Manipulation on horizontal space in Matplotlib subplots

To manipulate horizontal space in Matplotlib subplots, we can use the wspace parameter in the subplots_adjust() method. This parameter controls the amount of width reserved for space between subplots, expressed as a fraction of the average axis width.

Basic Syntax

fig.subplots_adjust(wspace=value)

Where value ranges from 0 (no space) to higher values (more space between subplots).

Example with Different Horizontal Spacing

Let's create subplots with actual plot data and demonstrate different horizontal spacing values −

import numpy as np
import matplotlib.pyplot as plt

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

# Create subplots with default spacing
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 6))

# Plot data on each subplot
ax1.plot(x, y1, 'b-', label='sin(x)')
ax1.set_title('sin(x)')
ax1.legend()

ax2.plot(x, y2, 'r-', label='cos(x)')
ax2.set_title('cos(x)')
ax2.legend()

ax3.plot(x, y3, 'g-', label='tan(x)')
ax3.set_title('tan(x)')
ax3.set_ylim(-2, 2)
ax3.legend()

ax4.plot(x, y4, 'm-', label='sin(x)*cos(x)')
ax4.set_title('sin(x)*cos(x)')
ax4.legend()

# Adjust horizontal space between subplots
fig.subplots_adjust(wspace=0.5)
plt.suptitle('Horizontal Space = 0.5')
plt.show()

Comparison of Different wspace Values

Here's how different wspace values affect the layout −

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 50)
y1 = np.sin(x)
y2 = np.cos(x)

# Create three figures with different wspace values
wspace_values = [0.1, 0.5, 1.0]

for i, wspace in enumerate(wspace_values):
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))
    
    ax1.plot(x, y1, 'b-')
    ax1.set_title('sin(x)')
    
    ax2.plot(x, y2, 'r-')
    ax2.set_title('cos(x)')
    
    fig.subplots_adjust(wspace=wspace)
    plt.suptitle(f'wspace = {wspace}')
    plt.show()

Key Parameters

Parameter Description Typical Range
wspace Horizontal spacing between subplots 0.0 - 2.0
hspace Vertical spacing between subplots 0.0 - 2.0

Combined with Vertical Space

You can adjust both horizontal and vertical spacing simultaneously −

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 50)

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

# Add plots to each subplot
ax1.plot(x, np.sin(x), 'b-')
ax1.set_title('sin(x)')

ax2.plot(x, np.cos(x), 'r-')
ax2.set_title('cos(x)')

ax3.plot(x, np.sin(2*x), 'g-')
ax3.set_title('sin(2x)')

ax4.plot(x, np.cos(2*x), 'm-')
ax4.set_title('cos(2x)')

# Adjust both horizontal and vertical spacing
fig.subplots_adjust(wspace=0.8, hspace=0.4)
plt.suptitle('Custom Horizontal and Vertical Spacing')
plt.show()

Conclusion

The wspace parameter in subplots_adjust() effectively controls horizontal spacing between subplots. Use values between 0.2-0.8 for optimal layout, and combine with hspace for complete spacing control.

---
Updated on: 2026-03-25T21:40:19+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements