170

I'm trying to share two subplots axes, but I need to share the x axis after the figure was created. E.g. I create this figure:

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(1000)/100.
x = np.sin(2*np.pi*10*t)
y = np.cos(2*np.pi*10*t)

fig = plt.figure()
ax1 = plt.subplot(211)
plt.plot(t,x)
ax2 = plt.subplot(212)
plt.plot(t,y)

# some code to share both x axes

plt.show()

Instead of the comment I want to insert some code to share both x axes. How do I do this? There are some relevant sounding attributes _shared_x_axes and _shared_x_axes when I check to figure axis (fig.get_axes()) but I don't know how to link them.

6 Answers 6

258

The usual way to share axes is to create the shared properties at creation. Either

fig=plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212, sharex = ax1)

or

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)

Sharing the axes after they have been created should therefore not be necessary.

However if for any reason, you need to share axes after they have been created (actually, using a different library which creates some subplots, like here might be a reason), there would still be a solution:

Using

ax2.sharex(ax1)

creates a link between the two axes, ax1 and ax2. In contrast to the sharing at creation time, you will have to set the xticklabels off manually for one of the axes (in case that is wanted).

A complete example:

import numpy as np
import matplotlib.pyplot as plt

t= np.arange(1000)/100.
x = np.sin(2*np.pi*10*t)
y = np.cos(2*np.pi*10*t)

fig=plt.figure()
ax1 = plt.subplot(211)
ax2 = plt.subplot(212)

ax1.plot(t,x)
ax2.plot(t,y)

ax2.sharex(ax1)
ax1.set_xticklabels([])
# ax2.autoscale() ## call autoscale if needed

plt.show()

For a list of axes you would do:

for ax in axes[1:]:
    ax.sharex(axes[0])
14
  • 10
    This is useful to connect select subplots. For example, a figure with 4 subplots: two time series and two histograms. This allows you to selectively link the time series.
    – Hamid
    Commented Sep 23, 2017 at 23:51
  • 4
    API docs for the Grouper object: matplotlib.org/2.0.2/api/… Commented Oct 25, 2017 at 12:06
  • 10
    Ohh, I just figured out how to unshare an axis (which can be useful in a large grid) - on that axis, do g = ax.get_shared_y_axes(); g.remove(a) for a in g.get_siblings(ax)]. Thanks for the starting point!
    – naught101
    Commented Mar 2, 2018 at 7:12
  • 6
    @naught101 You can just call ax2.autoscale(). Commented Mar 16, 2018 at 1:27
  • 12
    The .join method (matplotlib.cbook.GrouperView.join) is deprecated since version 3.6. How would you do without it ?
    – paime
    Commented Jan 26, 2023 at 8:46
64

As of Matplotlib v3.3 there now exist Axes.sharex, Axes.sharey methods:

ax1.sharex(ax2)
ax1.sharey(ax3)
2
  • 8
    note: you can only use Axes.sharex oncer per object. on the second call it errors out. so if you need to call it more than once use the more complex Axes.get_shared_x_axes().join(). Commented Mar 31, 2022 at 16:28
  • 2
    To get around this issue of only being able to call it once you could instead change the above example by doing what @mins suggested, and "Call ax2.sharey(ax1), ax3.sharey(ax1), and so on if required." Commented Jan 31 at 23:10
8

Just to add to ImportanceOfBeingErnest's answer above:

If you have an entire list of axes objects, you can pass them all at once and have their axes shared by unpacking the list like so:

ax_list = [ax1, ax2, ... axn] #< your axes objects 
ax_list[0].get_shared_x_axes().join(ax_list[0], *ax_list)

The above will link all of them together. Of course, you can get creative and sub-set your list to link only some of them.

Note:

In order to have all axes linked together, you do have to include the first element of the axes_list in the call, despite the fact that you are invoking .get_shared_x_axes() on the first element to start with!

So doing this, which would certainly appear logical:

ax_list[0].get_shared_x_axes().join(ax_list[0], *ax_list[1:])

... will result in linking all axes objects together except the first one, which will remain entirely independent from the others.

3
  • For everyone having trouble: add "autoscale", if it does not work (see the answers above)
    – Red One
    Commented Jan 19, 2022 at 15:56
  • 2
    The .join method (matplotlib.cbook.GrouperView.join) is deprecated since version 3.6. How would you do without it ?
    – paime
    Commented Jan 26, 2023 at 8:47
  • I can't help I'm afraid as I'm still on an earlier version. But I encourage you to post your solution, if you find one.
    – pfabri
    Commented Jan 27, 2023 at 11:00
3

Function join has been deprecated and will be removed soon. Continuing with this function is not recommended.

You can use the method suggested by iacob but, as commented by Trevor Boyd Smith, sharex and sharey can only be called once on the same object.

Thus the solution is to select one single axis as the argument of calls from multiple axes which need to be associated with the first one, e.g. to set the same y-scale for axes ax1, ax2 and ax3:

  • Select ax1 as the argument for other calls.
  • Call ax2.sharey(ax1), ax3.sharey(ax1), and so on if required.
1
  • Thanks! Currently one cannot make all data points appear with this method, i.e. the previous .join no longer works.
    – msch
    Commented Nov 27, 2023 at 14:58
2

Since the .get_shared_x_axes().join() method is deprecated, here is a function using ax.sharex() that also removes the tick labels of inner plots (as using sharex=True at construction time does) and works across figures:

def share_axes(axes, sharex=True, sharey=True):
    if isinstance(axes, np.ndarray):
        axes = axes.flat  # from plt.subplots
    elif isinstance(axes, dict):
        axes = list(axes.values())  # from plt.subplot_mosaic
    else:
        axes = list(axes)
    ax0 = axes[0]
    for ax in axes:
        if sharex:
            ax.sharex(ax0)
            if not ax.get_subplotspec().is_last_row():
                ax.tick_params(labelbottom=False)
        if sharey:
            ax.sharey(ax0)
            if not ax.get_subplotspec().is_first_col():
                ax.tick_params(labelleft=False)

Usage:

import matplotlib.pyplot as plt
import numpy as np

fig1, axes1 = plt.subplots(2, 2, figsize=(3, 3))
fig2, axes2 = plt.subplots(2, 2, figsize=(3, 3))

axes = [*axes1.flat, *axes2.flat]

for ax in axes:
    ax.imshow(np.random.randint(0, 255, size=(10, 10, 3)))

share_axes(axes)

plt.show()

Figures with shared axes

2

To share x-axes by column and y-axes by row across subplots in Matplotlib, you can use the sharex and sharey parameters with the values col and row respectively:

fig, axes = plt.subplots(2, 2, sharex='col', sharey='row')

Not the answer you're looking for? Browse other questions tagged or ask your own question.