Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to adjust the space between Matplotlib/Seaborn subplots for multi-plot layouts?
When creating multi-plot layouts with Matplotlib and Seaborn, controlling the spacing between subplots is essential for professional-looking visualizations. Python provides several methods to adjust subplot spacing effectively.
Using subplots_adjust() Method
The most common approach is using subplots_adjust() to control horizontal and vertical spacing between subplots.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
np.random.seed(42)
data1 = np.random.normal(0, 1, 100)
data2 = np.random.normal(2, 1.5, 100)
data3 = np.random.normal(-1, 0.8, 100)
data4 = np.random.normal(1, 1.2, 100)
# Set figure size
plt.figure(figsize=(10, 6))
# Create subplots
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
# Adjust spacing: hspace=vertical, wspace=horizontal
fig.subplots_adjust(hspace=0.3, wspace=0.3)
# Create plots
sns.boxplot(data=data1, ax=axes[0, 0])
axes[0, 0].set_title('Dataset 1')
sns.boxplot(data=data2, ax=axes[0, 1])
axes[0, 1].set_title('Dataset 2')
sns.boxplot(data=data3, ax=axes[1, 0])
axes[1, 0].set_title('Dataset 3')
sns.boxplot(data=data4, ax=axes[1, 1])
axes[1, 1].set_title('Dataset 4')
plt.show()
Using tight_layout() Method
For automatic spacing adjustment, tight_layout() provides a convenient solution ?
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
np.random.seed(42)
data = [np.random.normal(i, 1, 100) for i in range(4)]
# Create subplots
fig, axes = plt.subplots(2, 2, figsize=(10, 6))
# Use tight_layout for automatic spacing
plt.tight_layout(pad=2.0)
# Create plots with titles
for i, ax in enumerate(axes.flat):
sns.histplot(data=data[i], ax=ax, bins=20)
ax.set_title(f'Distribution {i+1}')
plt.show()
Using gridspec for Advanced Control
For more complex layouts, GridSpec offers precise control over subplot positioning ?
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns
import numpy as np
# Create sample data
np.random.seed(42)
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
data = np.random.normal(0, 1, 100)
# Create GridSpec with custom spacing
fig = plt.figure(figsize=(12, 8))
gs = gridspec.GridSpec(2, 2, hspace=0.4, wspace=0.3,
height_ratios=[1, 1], width_ratios=[1, 1])
# Create subplots using GridSpec
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, :]) # Span both columns
# Plot data
ax1.plot(x, y1)
ax1.set_title('Sine Wave')
ax2.plot(x, y2)
ax2.set_title('Cosine Wave')
sns.histplot(data=data, ax=ax3, bins=30)
ax3.set_title('Normal Distribution (Spanning Two Columns)')
plt.show()
Parameters Comparison
| Parameter | Description | Range | Default |
|---|---|---|---|
hspace |
Vertical spacing between subplots | 0.0 - 1.0 | 0.2 |
wspace |
Horizontal spacing between subplots | 0.0 - 1.0 | 0.2 |
pad |
Padding around subplots (tight_layout) | 0.0+ | 1.08 |
Common Use Cases
Small spacing: Use hspace=0.1, wspace=0.1 for tightly packed plots.
Medium spacing: Use hspace=0.3, wspace=0.3 for balanced layouts.
Large spacing: Use hspace=0.5, wspace=0.5 for well-separated plots.
Automatic: Use tight_layout() when you want matplotlib to handle spacing.
Conclusion
Use subplots_adjust() for precise control over subplot spacing. For automatic spacing, tight_layout() is convenient. Use GridSpec for complex multi-subplot layouts with different sizes.
