Skip to content

Axis swapping method? #6569

Closed
Closed

Description

@anntzer

I was wondering whether there was interest in working on an "axis-swapping" API, which would allow an Axes to use its x axis as a y axis and vice-versa (or at least figuring out whether this would be possible). The idea would be to replace all the plotting functions that possess variants where the two axes are swapped (hist(..., orientation="horizontal"), fill_between/fill_betweenx, etc.) as well as those for which such a variant is being proposed (#5856).

See also https://mail.python.org/pipermail/matplotlib-devel/2016-June/000472.html (and the earlier messages in the thread), which discuss the usefulness of sharing various pairs of axes among the three axes of a 3D plot with the two axes of a 2D plot (for an interactive orthogonal projection plot). I don't know how well 3D axes support sharing but perhaps this would again be much easier to support with "axes relabeling".

Activity

  1. added this to the milestone on Jun 11, 2016
  2. tacaswell commented on Jun 11, 2016

    @tacaswell
    Member

    It is worth investigating.

    Are you thinking a decorator-style thing flip_xy(plot(x, y, ...)) or an axes level ax.permute_axes(...)?

  3. anntzer commented on Jun 11, 2016

    @anntzer
    ContributorAuthor

    I think it should happen at the axes level, in order to allow for swapped axes sharing.

  4. sirimshin commented on Mar 26, 2018

    @sirimshin

    Do you have any examples of how it would work?

  5. anntzer commented on Mar 26, 2018

    @anntzer
    ContributorAuthor
    ax.plot(x0, y0)
    ax.hist(..., orientation="horizontal")
    ax.swap_axes()
    ax.plot(x1, y1)
    ax.hist(..., orientation="horizontal")
    

    should be equivalent to

    ax.plot(x0, y0)
    ax.hist(..., orientation="horizontal")
    # <nothing>
    ax.plot(y1, x1)
    ax.hist(..., orientation="vertical")
    
  6. timhoffm commented on Mar 27, 2018

    @timhoffm
    Member

    So swap_axes() will just change the meaning of x and y args and some kwargs of future plots? After removing the obsolete parameter orientation, your example would end up like:

    ax.plot(x0, y0)
    ax.hist(...)
    ax.swap_axes()
    ax.plot(x1, y1)
    ax.hist(...)
    

    Just a few spontaneous thoughts:

    • I'm not a big fan of the orientation state introduced with this concept. I have to know if swap_axes has been called earlier to know what my plot function will do.
    • It starts to get messy with additional parameters, e.g. align = {left, center, right} vs. align = {bottom, center, top}. Would need conditional checks on the allowed values and explanatory docstrings, that they depend on the orientation state.
    • an orientation parameter is probably easier to understand than swap_axes (e.g. does it swap existing plot elements or only the behavior of future plot commands?).
    • It's probably difficult to apply it to a complete consistent set of functions. Does it influence texts/annotations, hlines/vlines, added artists and patches?

    On the first impression, this looks quite rabbit-holy.

  7. anntzer commented on Mar 27, 2018

    @anntzer
    ContributorAuthor

    Oh, I definitely agree it'd be kind of a mess :-)

  8. QuLogic commented on Mar 27, 2018

    @QuLogic
    Member

    Huh, I'd have thought you meant the opposite,

    ax.plot(y0, x0)
    ax.hist(..., orientation="vertical")
    # <nothing>
    ax.plot(x1, y1)
    ax.hist(..., orientation="horizontal")

    i.e., it swaps the Axis and all existing plotted things, but IIRC that's way way harder.

  9. anntzer commented on Mar 27, 2018

    @anntzer
    ContributorAuthor

    Well, that's a valid interpretation too... (but yes that looks even harder than my proposal)

  10. modified the milestones: v3.0, needs sorting on Jul 26, 2018
  11. andrzejnovak commented on Sep 10, 2020

    @andrzejnovak
    Contributor

    IMO it would be a very nice utility to have. I would expect the latter behaviour, swapping/orientation to be essentially a property of the axes object, so that it would really only "happen" at draw time. Any other interaction with the axes (i.e. plotting) should be "unaware" of it.

  12. jklymak commented on Sep 10, 2020

    @jklymak
    Member

    The first case is really hard, the second case is almost impossible, both involving a bunch of state being carried around both by matplotlib and the user in their head, and all just to save the user a bit of cut and paste? I'm 👎 on this.

  13. timhoffm commented on Sep 10, 2020

    @timhoffm
    Member

    While the idea sounds fancy, it is "too clever". I do not think it's possible to implement this in a consistent way across the library. See my comment at #6569 (comment).

    From an API perspective, I'm also not convinced that swapping axes (either for modifying future drawings ore for flipping the existing plot) is an intuitive way of working. People think of the figure a some sort of paper to draw on, and you'd rather say "draw horizontal bar plot" but not "rotate your paper and draw a bar plot".

    I'm closing this as non feasible. If anybody wants to advocate this nevertheless, the way forward would be a working implementation for a number existing "rotated" plotting functions (barh(), fill_betweenx(), ...) to use it internally without already exposing this as public API. Such an implementation (if possible) would simplify these functions and be worth it, on its own. Whether it's something to add to the public API would need a separate discussion.

  14. anntzer commented on Sep 10, 2020

    @anntzer
    ContributorAuthor

    Let's close this as likely too hard; if someone really manages to come up with a reasonable implementation we can always revisit.

  15. naught101 commented on May 28, 2021

    @naught101

    FWIW, this would be particularly useful when used with pandas, which has df.plot(), which (with the right type of data), plots lines (similar to plotting a array/list of lists). Transposing the data first is not possible, because the column headers are used for the line labels, and so that data is lost. Being able to flip the data and maintain those labels would be very useful. Though perhaps this is an issue for pandas...

  16. removed this from the future releases milestone on Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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