Course contentsShow
← Data Science
Lesson 1276 of 2,14532. Visualization with Matplotlib and SeabornFree lesson

Sharing Axes Between Subplots

Use sharex and sharey parameters to link axes across subplots for consistent scales and easier comparison.

Sharing Axes Between Subplots

You'll learn how to link axes across subplots so they use the same scale, making visual comparisons much easier.

What Does Sharing Axes Mean?

When you create multiple subplots, each typically has its own independent axes with different scales. But sometimes you want to compare patterns across subplots directly—and that's hard when each uses different ranges. Sharing axes means linking the x-axis, y-axis, or both across multiple subplots so they all use identical scales.

Think of it like synchronizing rulers: if you're measuring different objects side-by-side, using rulers with the same units and scale makes comparison straightforward. Without shared scales, you might think one pattern is bigger simply because its subplot zoomed in more.

How to Share Axes

When calling plt.subplots(), use the sharex and sharey parameters:

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.sin(x) * 2

# Share y-axis across subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True)

axes[0].plot(x, y1)
axes[0].set_title('Sin(x)')

axes[1].plot(x, y2)
axes[1].set_title('2*Sin(x)')

plt.show()

With sharey=True, both subplots now use the same y-axis range. This makes it immediately obvious that the second wave has twice the amplitude. You can also use sharex=True for horizontal alignment or set both to share all axes.

When to Use This

  • Time series comparisons: multiple metrics over the same time period
  • Distribution comparisons: ensuring all histograms use the same bin ranges
  • Multi-panel dashboards: where consistent scales prevent visual confusion

Key Takeaway: Use sharex and sharey parameters in plt.subplots() to synchronize axis scales across subplots, enabling fair visual comparisons and reducing viewer confusion.