72

I am having a big plot where I initiated with:

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots(5, 4)

And I want to do share-x-axis between column 1 and 2; and do the same between column 3 and 4. However, column 1 and 2 does not share the same axis with column 3 and 4.

I was wondering that would there be anyway to do this, and not sharex=True and sharey=True across all figures?

PS: This tutorial does not help too much, because it is only about sharing x/y within each row/column; they cannot do axis sharing between different rows/columns (unless share them across all axes).

4 Answers 4

72

I'm not exactly sure what you want to achieve from your question. However, you can specify per subplot which axis it should share with which subplot when adding a subplot to your figure.

This can be done via:

import matplotlib.pylab as plt

fig = plt.figure()

ax1 = fig.add_subplot(5, 4, 1)
ax2 = fig.add_subplot(5, 4, 2, sharex = ax1)
ax3 = fig.add_subplot(5, 4, 3, sharex = ax1, sharey = ax1)
5
  • 1
    Thank you! That really helps. I looked and it does not seem that there is a method of the axes object that can set the sharex/sharey property. Do you think that's the case? May 8, 2014 at 0:10
  • I haven't been able to find something so I assume so. I do need to note that my search skills are good but not the best.
    – The Dude
    May 9, 2014 at 7:54
  • 3
    How does it work with axis arrays does ax[1,0] = plot(x,y) ax[1,1] = plot(x,y,sharey=ax[1,0]) . This does not work
    – AAI
    Sep 8, 2017 at 16:36
  • 1
    That should be the accepted answer. Don't understand why it is so far down the list. (I guess because it is sorted by Active first). Oct 1, 2020 at 8:12
  • It works for axis arrays too - make sure you use np.empty() for the array, e.g.: ax = np.empty((4, 2), dtype=object); fig = plt.figure(); ax[0][0] = fig.add_subplot(4, 2, 1, sharex=ax[0][0]); ax[0][1] = fig.add_subplot(4, 2, 2, sharex=ax[0][0]) Apr 4 at 17:08
68

A slightly limited but much simpler option is available for subplots. The limitation is there for a complete row or column of subplots. For example, if one wants to have common y axis for all the subplots but common x axis only for individual columns in a 3x2 subplot, one could specify it as:

import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 2, sharey=True, sharex='col')
4
  • 2
    This is lovely! Thank you!!
    – raulindo
    Jul 15, 2021 at 22:10
  • 3
    This really is perfect. Never knew it was a thing until now! Sep 22, 2021 at 4:37
  • Really nice feature! Mar 21, 2022 at 9:55
  • 1
    Perfect! And the equivalent sharey='row'
    – tiagoams
    Oct 16 at 10:59
18

One can manually manage axes sharing using a Grouper object, which can be accessed via ax._shared_axes['x'] and ax._shared_axes['y']. For example,

import matplotlib.pyplot as plt

def set_share_axes(axs, target=None, sharex=False, sharey=False):
    if target is None:
        target = axs.flat[0]
    # Manage share using grouper objects
    for ax in axs.flat:
        if sharex:
            target._shared_axes['x'].join(target, ax)
        if sharey:
            target._shared_axes['y'].join(target, ax)
    # Turn off x tick labels and offset text for all but the bottom row
    if sharex and axs.ndim > 1:
        for ax in axs[:-1,:].flat:
            ax.xaxis.set_tick_params(which='both', labelbottom=False, labeltop=False)
            ax.xaxis.offsetText.set_visible(False)
    # Turn off y tick labels and offset text for all but the left most column
    if sharey and axs.ndim > 1:
        for ax in axs[:,1:].flat:
            ax.yaxis.set_tick_params(which='both', labelleft=False, labelright=False)
            ax.yaxis.offsetText.set_visible(False)

fig, axs = plt.subplots(5, 4)
set_share_axes(axs[:,:2], sharex=True)
set_share_axes(axs[:,2:], sharex=True)

To adjust the spacing between subplots in a grouped manner, please refer to this question.

EDIT: Modified the code according to the latest matplotlib API updates. Thanks to @Jonvdrdo 's suggestions!

3
  • Not directly applicable at my use case (sharing x-axis between row 2n and row 2n+1, and sharing y-axis of all even-numbered rows on the one hand, and all odd-numbered rows on the other hand, for an 8 rows by 2 columns figure), but I could adapt it. I didn't know of Grouper, nice!
    – Aubergine
    Oct 20, 2021 at 3:34
  • 2
    The Matplotlib API has been updated, see this link. As a result, you need to change the if sharex content to target._shared_axes['x'].join(target, ax) and if sharey to target._shared_axes['y'].join(target, ax). Then it works for later Matplotlib versions (tested on Matplotlib==3.5.1)
    – Jonvdrdo
    Jan 18, 2022 at 15:10
  • Note: In the past (<3.8) one could also access the target._shared_axes['x'] and target._shared_axes['y'] via get_shared_x_axes() and get_shared_y_axes(), which, imho, looks better than using the "private" methods. BUT now these return immutable GrouperView objects. So you need to either go the way of target._shared_axes or target.get_shared_x_axes()._grouper` . I am not sure what they (at mpl) were thinking here.
    – Joschua
    Sep 20 at 13:22
3

I used Axes.sharex /sharey in a similar setting

https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.sharex.html#matplotlib.axes.Axes.sharex

import matplotlib.pyplot as plt
fig, axd = plt.subplot_mosaic([list(range(3))] +[['A']*3, ['B']*3])

axd[0].plot([0,0.2])
axd['A'].plot([1,2,3])
axd['B'].plot([1,2,3,4,5])


axd['B'].sharex(axd['A'])

for i in [1,2]:
    axd[i].sharey(axd[0])
plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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