FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support simple axes shares in subplot_mosaic · Issue #18305 · matplotlib/matplotlib · GitHub

Repository navigation

Support simple axes shares in subplot_mosaic #18305

Description

Problem

I realized that subplot_mosaic() is quite nice even to create single row or single column layouts if you were previously going to stuff the result of subplots() in a dict anyways (or use hardcoded indices, in which case a dict may be more robust if you may (in some later iteration of the code) add axes somewhere in the middle).

(Side points: Also, this avoids running into the subtle bug, when writing axs = subplots(n), of the case n = 1 where axs is squeezed into a single axes (yes, I know, the fix is to pass squeeze=False...) Single column is slightly trickier than single row (subplot_mosaic([[name] for name in names])) but heh.).

However, subplot_mosaic() does not allow axes sharing. Yes, I've even argued against it in the original PR on the grounds of "too complicated" (#16603 (comment))...

Proposed Solution

... but we could at least support the simplest case(s), as in subplots(): sharex/sharey=True/"all" (this is completely unambiguous: share all axes -- "all" is a synonym from subplots() that we probably want to keep for consistency), and possibly "row"/"col" (I'd say two axes (which may have various spans) are in the same row for sharing purposes if they both begin on the same row and end on the same row, which seems the most useful definition)? We should be careful, when checking for True, to actually check for that value (or 1, or np.bool(True)), and not for general truthiness, so that we don't get boxed in later if we decide we do want to support passing in complex sharing specs (e.g. via dicts, as proposed in the original PR thread). (I'm still against that complexity, at least for now...)

Labeling as good first issue as I don't think there's too much complexity or API design space here, although we still need to decide whether this is a good idea or not.

Additional context and prior art

Activity

  1. dopplershift commented on Aug 21, 2020

    Contributor

    I'm half-way tempted to remove "good first issue" until we actually decide this is a good idea...

  2. timhoffm commented on Aug 21, 2020

    Member

    I'm ok with the proposal. It's a clear specification and can help for simple layouts. I don't expect it to be useful for really complex layout, but it would do still do something reasonable there.

  3. Selich commented on Sep 24, 2020

    I'm currently reading the documentation and I didn't know, until now, that it does not have sharex/sharey parameters. Will look at the code a bit and send a PR if I figure it out.

    Is it still ok to work on this issue?

  4. timhoffm commented on Sep 25, 2020

    Member

    Yes, you can work on this issue. I support the sharing semantics proposed by @anntzer above.

  5. added this to the v3.4.0 milestone on Sep 25, 2020
  6. aitikgupta commented on Jan 8, 2021

    Contributor

    Hey @Selich are you still planning to work on this?

  7. Selich commented on Jan 8, 2021

  8. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  9. tacaswell commented on Jun 30, 2021

    Member

    Closed by #20107

  10. mwaskom commented on Oct 3, 2021

    IMHO this is not fully closed because it looks like #20107 doesn't support "col"/"row" as arguments.

  11. reopened this on Oct 3, 2021
  12. 9 remaining items

  13. added this to the v3.7.3 milestone on Jul 5, 2023
  14. ketozhang commented on Jul 15, 2023

    Can I take a stab on the remaining work (currently at SciPy 2023 sprint)?

  15. modified the milestones: v3.7.3, v3.8.0 on Sep 9, 2023
  16. removed this from the v3.8.0 milestone on Sep 15, 2023
  17. story645 commented on Aug 20, 2026

    Member

    would it be very unwieldy to support dicts? That's more or less a simplified version of your comment: (ax["A"].sharex([ax["B"], ax["C"]])), -> {'A': ['B', 'C']}

  18. tacaswell commented on Aug 21, 2026

    Member

    Is the key:value adding anything? I think a list of lists would also do the job sharex=[[A, B], [C, d]] without making you pick one of the axes as "special" to act as the key.

  19. story645 commented on Aug 21, 2026

    Member

    Is the key:value adding anything?

    I was thinking there's a controlling axis, but I guess not. But also sounds like implementing either would first require extending sharex/sharey as apparently they've got an explicit limit of 1 share:

  20. tacaswell commented on Aug 21, 2026

    Member

    We can carry code like (which assumes we have done the key -> axes object mapping already)

    for ax_list in sharex:
       head, *rest = ax_list
       for ax in rest:
           head.sharex(ax)

    in subplot mosaic. We already have a very similar loop for the "all" case.

  21. story645 commented on Aug 21, 2026

    Member

    Realizing the share docs are just confusingly written. Still think it would be a good idea to generalize share but that can be a separate discussion then.

  22. story645 commented on Aug 21, 2026

    Member

    Never mind, that loop won't work if I'm testing this right: (using mpl-dev environment)

    In [4]: fig, axes = plt.subplots(4)
    
    In [6]: axes[0].sharex(axes[1])
    
    In [7]: axes[0].sharex(axes[2])
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    Cell In[7], line 1
    ----> 1 axes[0].sharex(axes[2])
    
    File ~\Projects\matplotlib\lib\matplotlib\axes\_base.py:1313, in _AxesBase.sharex(self, other)
       1311 _api.check_isinstance(_AxesBase, other=other)
       1312 if self._sharex is not None and other is not self._sharex:
    -> 1313     raise ValueError("x-axis is already shared")
       1314 self._shared_axes["x"].join(self, other)
       1315 self._sharex = other
    
    ValueError: x-axis is already shared

    You have to do the reverse:

    In [8]: fig, axes = plt.subplots(4)
    
    In [9]: axes[0].sharex(axes[2])
    
    In [10]: axes[1].sharex(axes[2])
  23. timhoffm commented on Aug 21, 2026

    Member

    Sharing is implemented asymmetrically/directionally (see also #30159 (comment)). This is an implementation detail and not logically necessary.

    IMHO we should remove that and only store the shared state in the grouper. See #30159 (comment)

  24. Gambit-Checkmate commented on Aug 22, 2026

    Hi, I’m interested in helping with simple axes sharing in subplot_mosaic. I’d first compare the existing sharex/sharey handling and add focused tests for the requested layout cases before proposing the smallest API change. Is this issue still open for community work?

  25. Stuaarts commented on Aug 31, 2026

    Hi, I'd like to work on this issue as my first contribution. Is it still available ? If so, Could you please assign the issue to me? Thank you !

  26. timhoffm commented on Sep 5, 2026

    Member

    We do not assign issues. Please see our contribution guide https://matplotlib.org/devdocs/devel/contribute.html#managing-issues-prs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    New feature🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL