Skip to content

[ENH]: sharex=array for simple grouping #30241

Open
@bmcfee

Description

@bmcfee

Problem

When creating subplots and using the sharex/sharey parameters, we currently have four basic options: all, row, column, or none.

If we want more targeted axis sharing, it can be done explicitly by calling axes[i].sharex(axes[j]). This works fine, but it can be a little tedious, especially when we have many groups of subplots that we'd like to share.

For example, if we have a 2x4 subplot grid like

ABCD
EFGH

where we want ABC to share an axis but not with D, and again for EFG but not H. (Maybe D and H represent some marginal summaries of the preceding plots on their row, and the scales should not be shared.) It's rather cumbersome to step through and manually share axes.

Proposed solution

What if we were able to provide an array that codes each axes into a group, which could be accepted as a value for sharex=? Example as in the case above:

groups = np.array([[1, 1, 1, 2], [3, 3, 3, 4]])
fig, ax = plt.subplots(nrows=2, ncols=4, sharey=groups)

# Now in the aforementioned grid, we have y-axis shared for A/B/C, 
# and separately shared for E/F/G.
# All other axes are independent.

This would require that the provided group array has a hash-friendly dtype (eg int) and the shape matches the specified rows and columns. The implementation from within subplots() would be pretty simple, amounting to a lookup to see if any other axes share the same group id.


A final note on this: I actually have implemented this idea already in a slightly different context for identifying subplot axes to share other attributes (facecolor in my case), and I'm finding it pretty natural and comfortable to use.

The original motivating use-case I had involved plotting arrays of time-domain signals, which all share a common time axis (column), but only certain groups have common vertical scales. E.g., channels 1 and 2 come from one source, 3 and 4 from another, etc.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions