How to set same scale for subplots in Python using Matplotlib?

When creating subplots in Matplotlib, you often want them to share the same scale for better comparison. This is achieved using the sharex and sharey parameters when creating subplot arrangements.

Using sharex Parameter

The sharex parameter links the x-axis scale across subplots. When you zoom or pan one subplot, all linked subplots will follow ?

import matplotlib.pyplot as plt
import numpy as np

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

# Create figure
fig = plt.figure()

# Add subplots with shared x-axis
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax1)

# Create data points
t = np.linspace(-5, 5, 100)

# Plot sine and cosine curves
ax1.plot(t, np.sin(2 * np.pi * t), color='red', lw=3, label='sin(2?t)')
ax2.plot(t, np.cos(2 * np.pi * t), color='blue', lw=3, label='cos(2?t)')

# Add labels
ax1.set_ylabel('Amplitude')
ax1.legend()
ax2.set_ylabel('Amplitude')
ax2.set_xlabel('Time')
ax2.legend()

plt.show()

Using plt.subplots() with Shared Axes

The plt.subplots() function provides a more convenient way to create multiple subplots with shared scales ?

import matplotlib.pyplot as plt
import numpy as np

# Create subplots with shared x and y axes
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(8, 6), 
                               sharex=True, sharey=True)

# Generate data
x = np.linspace(0, 10, 100)
y1 = np.exp(-x/3) * np.cos(x)
y2 = np.exp(-x/4) * np.sin(x)

# Plot data
ax1.plot(x, y1, 'g-', linewidth=2)
ax1.set_title('Damped Cosine')
ax1.grid(True)

ax2.plot(x, y2, 'r-', linewidth=2)
ax2.set_title('Damped Sine')
ax2.set_xlabel('X values')
ax2.grid(True)

plt.tight_layout()
plt.show()

Sharing Different Axis Combinations

You can control which axes are shared using different parameter combinations ?

import matplotlib.pyplot as plt
import numpy as np

# Create 2x2 subplots with different sharing options
fig, axes = plt.subplots(2, 2, figsize=(10, 8),
                        sharex='col',  # Share x-axis within columns
                        sharey='row')  # Share y-axis within rows

# Generate sample data
x = np.linspace(0, 5, 50)
data = [x, x**2, np.sin(x), np.cos(x)]
titles = ['Linear', 'Quadratic', 'Sine', 'Cosine']
colors = ['blue', 'red', 'green', 'orange']

# Plot in each subplot
for i, ax in enumerate(axes.flat):
    ax.plot(x, data[i], color=colors[i], linewidth=2)
    ax.set_title(titles[i])
    ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

Comparison of Sharing Options

Parameter Effect Best For
sharex=True All subplots share x-axis Time series comparisons
sharey=True All subplots share y-axis Magnitude comparisons
sharex='col' Subplots in same column share x-axis Grid layouts with vertical alignment
sharey='row' Subplots in same row share y-axis Grid layouts with horizontal alignment

Conclusion

Use sharex and sharey parameters to synchronize scales across subplots. The plt.subplots() function offers the most convenient approach for creating multiple subplots with shared axes, improving data comparison and visualization consistency.

Updated on: 2026-03-26T02:28:39+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements