# Share Axes Between Matplotlib Subplots (sharex, sharey, and Common Labels)

> How to share axes in Matplotlib subplots with sharex and sharey, add a common x and y label with supxlabel and supylabel, one shared legend, and restore tick labels.

Source: https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/ | Published: 2026-11-27 | Author: Alex Scott | Category: Matplotlib | Tags: matplotlib, subplots, sharex, sharey, axis labels, legend

Pass `sharex=True` and `sharey=True` to `plt.subplots()` so every panel uses the same axis limits, then use `fig.supxlabel()` and `fig.supylabel()` for one common label. Matplotlib also hides the repeated inner tick labels.

## Quick Example

```python
import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(44)
hour = np.arange(0, 72)
stations = {"Upper weir": 2.9, "Mill bridge": 2.1, "Town quay": 1.6, "Estuary": 0.9}
levels = {
    name: base + 0.9 * base * np.exp(-((hour - peak) / 11) ** 2) + rng.normal(0, 0.05, 72)
    for (name, base), peak in zip(stations.items(), [20, 28, 35, 46])
}

fig, axes = plt.subplots(2, 2, figsize=(9, 5.4), sharex=True, sharey=True,
                         layout="constrained")
for ax, (name, level) in zip(axes.flat, levels.items()):
    ax.plot(hour, level, color="#0e7490")
    ax.set_title(name)

fig.supxlabel("Hours since the storm began")
fig.supylabel("River level (m)")
plt.show()
```

![Four river level subplots with shared x and y axes, one common x label and one common y label](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-quick-example.png)

All four panels share one y range, so you can see that the upper weir reaches the highest level and the estuary peaks last. The code targets Matplotlib 3.11.

## Why Share Axes?

Without sharing, each subplot picks its own limits and a 1.7 m peak looks as tall as a 5.5 m one. Here are two of the stations drawn both ways:

```python
fig = plt.figure(figsize=(9, 5), layout="constrained")
top, bottom = fig.subfigures(2, 1)

for subfig, share, title in ((top, False, "sharey=False (default)"),
                             (bottom, True, "sharey=True")):
    axes = subfig.subplots(1, 2, sharey=share)
    for ax, name in zip(axes, ["Upper weir", "Estuary"]):
        ax.plot(hour, levels[name], color="#0e7490")
        ax.set_title(name)
    subfig.suptitle(title, fontweight="bold")
plt.show()
```

![Comparison of the same two river stations with independent y axes, where the peaks look similar, and with a shared y axis, where the estuary is much lower](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-compare.png)

## Share by Row or Column

`sharex` and `sharey` also accept `"col"` and `"row"`. Use them when columns measure different things or rows have different units.

| Value | Axes are shared with |
|---|---|
| `False` or `"none"` | Nothing (default) |
| `True` or `"all"` | Every subplot |
| `"col"` | Subplots in the same column |
| `"row"` | Subplots in the same row |

```python
day = np.arange(1, 31)
rainfall = rng.gamma(1.2, 6, (2, 30))
flow = 20 + np.array([np.convolve(r, [0.5, 0.8, 0.5, 0.2])[:30] for r in rainfall])

fig, axes = plt.subplots(2, 2, figsize=(9, 5), sharex="col", sharey="row",
                         layout="constrained")
for col, river in enumerate(["North fork", "South fork"]):
    axes[0, col].bar(day, rainfall[col], color="#6366f1")
    axes[1, col].plot(day, flow[col], color="#0f766e")
    axes[0, col].set_title(river)
    axes[1, col].set_xlabel("Day")
axes[0, 0].set_ylabel("Rain (mm)")
axes[1, 0].set_ylabel("Flow (m³/s)")
plt.show()
```

![Two by two grid with rainfall bars on the top row and river flow lines on the bottom row, sharing x by column and y by row](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-row-col.png)

## Show Tick Labels on Shared Axes

Sharing hides tick labels on the inner panels. Turn them back on for a particular Axes with `tick_params()`:

```python
fig, axes = plt.subplots(1, 3, figsize=(9, 2.8), sharey=True, layout="constrained")
for ax, name in zip(axes, ["Upper weir", "Mill bridge", "Town quay"]):
    ax.plot(hour, levels[name], color="#0e7490")
    ax.set_title(name)

for ax in axes[1:]:
    ax.tick_params(labelleft=True)  # use labelbottom=True for a shared x axis
plt.show()
```

![Three river level subplots with a shared y axis and tick labels shown on every panel](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-tick-labels.png)

For a grid you built without sharing, `ax.label_outer()` does the opposite and hides the inner labels. It only hides text, so set matching limits yourself if the panels should be comparable.

## Share Axes Between Specific Subplots

To link only some panels, call `ax.sharex(other)` or `ax.sharey(other)`, or pass `sharex=other` to `fig.add_subplot()`. This also works with [`subplot_mosaic` and GridSpec layouts](https://www.pythoncharts.com/matplotlib/matplotlib-subplot-mosaic-gridspec/).

```python
fig, axd = plt.subplot_mosaic("AAB;CCB", figsize=(9, 4.6), layout="constrained")
axd["C"].sharex(axd["A"])

axd["A"].plot(hour, levels["Upper weir"], color="#0e7490")
axd["A"].set_title("Upper weir")
axd["A"].tick_params(labelbottom=False)
axd["C"].plot(hour, levels["Estuary"], color="#b45309")
axd["C"].set_title("Estuary")
axd["B"].barh(list(levels), [level.max() for level in levels.values()], color="#64748b")
axd["B"].invert_yaxis()
axd["B"].tick_params(axis="y", labelrotation=90)
axd["B"].set_title("Peak level (m)")

axd["A"].set_xlim(0, 60)  # C follows
plt.show()
```

![Mosaic layout where two time series panels share an x axis from 0 to 60 hours and a separate bar chart of peak levels does not](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-selected.png)

`sharex()` doesn't hide any tick labels, hence the `tick_params()` call. An Axes can be joined to a shared group only once per direction; a second call raises `ValueError: x-axis is already shared`. In a mosaic, `sharex=True` links every panel, including the bar chart here, which is rarely what you want when panels differ.

## Stack Panels with No Gap

For a stack of time series, set `hspace=0` so the panels touch. Constrained layout overrides `hspace` from `gridspec_kw`, so leave `layout` unset for this one:

```python
fig, axes = plt.subplots(4, 1, figsize=(8, 5.5), sharex=True, gridspec_kw={"hspace": 0})
for ax, (name, level) in zip(axes, levels.items()):
    ax.fill_between(hour, level, color="#a5f3fc")
    ax.plot(hour, level, color="#0e7490")
    ax.set_ylim(0, 6.5)
    ax.set_yticks([0, 2, 4])
    ax.text(0.99, 0.85, name, transform=ax.transAxes, ha="right", va="top")

axes[-1].set_xlabel("Hour")
fig.supylabel("River level (m)")
plt.show()
```

![Four river level area charts stacked with no gap on one shared x axis](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-stacked.png)

## One Legend for All Subplots

When every panel uses the same series, collect the handles from one Axes and call `fig.legend()`. The `"outside lower center"` location needs `layout="constrained"`.

```python
fig, axes = plt.subplots(1, 3, figsize=(9, 3.4), sharey=True, layout="constrained")
for ax, name in zip(axes, ["Upper weir", "Mill bridge", "Town quay"]):
    ax.plot(hour, levels[name], color="#0e7490", label="Observed")
    ax.axhline(stations[name] * 1.5, color="#dc2626", ls="--", label="Flood warning")
    ax.set_title(name)

handles, labels = axes[0].get_legend_handles_labels()
fig.legend(handles, labels, loc="outside lower center", ncols=2)
fig.supylabel("River level (m)")
plt.show()
```

![Three river subplots with a dashed flood warning line in each and a single legend centered below the figure](https://www.pythoncharts.com/matplotlib/matplotlib-shared-axes-subplots/images/matplotlib-shared-axes-legend.png)

More placement options are in the guide to [legends outside the plot](https://www.pythoncharts.com/matplotlib/matplotlib-legend-outside/).

## Practical Tips

- Limits, scales, and zooming apply to the whole shared group. `ax.set_yscale("log")` on one panel changes all of them.
- Autoscaling covers the data from every shared panel, so one outlier stretches them all.
- Two measures on the same panel with different units need [dual y-axes](https://www.pythoncharts.com/matplotlib/matplotlib-dual-y-axes-twinx/), not `sharey`.
- Font size and padding for common labels work as for any [axis label](https://www.pythoncharts.com/matplotlib/matplotlib-axis-labels/): `fig.supxlabel("Hour", fontsize=13)`.

## Similar Topics

- [Matplotlib Subplots](https://www.pythoncharts.com/matplotlib/matplotlib-subplots/)
- [Matplotlib Axis Labels](https://www.pythoncharts.com/matplotlib/matplotlib-axis-labels/)
- [subplot_mosaic and GridSpec Layouts](https://www.pythoncharts.com/matplotlib/matplotlib-subplot-mosaic-gridspec/)
