Skip to content

Axis swapping method? #6569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anntzer opened this issue Jun 10, 2016 · 13 comments
Closed

Axis swapping method? #6569

anntzer opened this issue Jun 10, 2016 · 13 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Jun 10, 2016

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".

@tacaswell tacaswell added this to the 3.0 (future) milestone Jun 11, 2016
@tacaswell
Copy link
Member

It is worth investigating.

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

@anntzer
Copy link
Contributor Author

anntzer commented Jun 11, 2016

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

@sirimshin
Copy link

Do you have any examples of how it would work?

@anntzer
Copy link
Contributor Author

anntzer commented Mar 26, 2018

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")

@timhoffm
Copy link
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.

@anntzer
Copy link
Contributor Author

anntzer commented Mar 27, 2018

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

@QuLogic
Copy link
Member

QuLogic commented Mar 27, 2018

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.

@anntzer
Copy link
Contributor Author

anntzer commented Mar 27, 2018

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

@dstansby dstansby modified the milestones: v3.0, needs sorting Jul 26, 2018
@andrzejnovak
Copy link
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.

@jklymak
Copy link
Member

jklymak commented Sep 10, 2020

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.

@timhoffm
Copy link
Member

timhoffm commented Sep 10, 2020

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.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 10, 2020

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

@anntzer anntzer closed this as completed Sep 10, 2020
@naught101
Copy link

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...

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

No branches or pull requests

10 participants