Setting the same axis limits for all subplots in Matplotlib

Setting the same axis limits for all subplots in Matplotlib ensures consistent scaling across multiple plots. You can achieve this using sharex and sharey parameters or by explicitly setting limits on each subplot.

Method 1: Using sharex and sharey Parameters

The most efficient approach is to share axes between subplots ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

# Create first subplot and set limits
ax1 = plt.subplot(2, 2, 1)
ax1.set_xlim(0, 5)
ax1.set_ylim(0, 5)
ax1.plot([1, 4, 3], 'b-o', label='Plot 1')
ax1.set_title('Subplot 1')

# Share axes with the first subplot
ax2 = plt.subplot(2, 2, 2, sharex=ax1, sharey=ax1)
ax2.plot([3, 4, 1], 'r-s', label='Plot 2')
ax2.set_title('Subplot 2')

ax3 = plt.subplot(2, 2, 3, sharex=ax1, sharey=ax1)
ax3.plot([4, 0, 4], 'g-^', label='Plot 3')
ax3.set_title('Subplot 3')

ax4 = plt.subplot(2, 2, 4, sharex=ax1, sharey=ax1)
ax4.plot([2, 4, 2], 'm-d', label='Plot 4')
ax4.set_title('Subplot 4')

plt.tight_layout()
plt.show()

Method 2: Using plt.subplots() with Global Sharing

Create all subplots at once with shared axes ?

import matplotlib.pyplot as plt
import numpy as np

# Create subplots with shared axes
fig, axes = plt.subplots(2, 2, figsize=(10, 6), sharex=True, sharey=True)

# Sample data
data = [
    [1, 4, 3],
    [3, 4, 1], 
    [4, 0, 4],
    [2, 4, 2]
]

colors = ['blue', 'red', 'green', 'purple']
markers = ['o', 's', '^', 'd']

# Plot on each subplot
for i, ax in enumerate(axes.flat):
    ax.plot(data[i], color=colors[i], marker=markers[i], linewidth=2)
    ax.set_title(f'Subplot {i+1}')
    ax.grid(True, alpha=0.3)

# Set common limits (optional - can be set on any axis)
axes[0, 0].set_xlim(0, 5)
axes[0, 0].set_ylim(0, 5)

plt.tight_layout()
plt.show()

Method 3: Manually Setting Limits on Each Subplot

Explicitly set the same limits on all subplots ?

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(2, 2, figsize=(10, 6))

# Define common limits
x_min, x_max = 0, 5
y_min, y_max = 0, 5

data = [[1, 4, 3], [3, 4, 1], [4, 0, 4], [2, 4, 2]]

for i, ax in enumerate(axes.flat):
    ax.plot(data[i], 'o-', linewidth=2)
    ax.set_xlim(x_min, x_max)  # Set same x limits
    ax.set_ylim(y_min, y_max)  # Set same y limits
    ax.set_title(f'Subplot {i+1}')
    ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

Comparison of Methods

Method Best For Advantages Limitations
sharex/sharey Interactive plots Automatic synchronization All subplots must share limits
plt.subplots() Simple grid layouts Clean, concise code Less control over individual plots
Manual limits Custom layouts Full control over each subplot More code, potential inconsistency

Conclusion

Use sharex=True, sharey=True in plt.subplots() for the cleanest approach. For existing subplots, use sharex and sharey parameters when creating each subplot to automatically maintain consistent axis limits across all plots.

Updated on: 2026-03-25T21:14:28+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements