-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add step option where='edges' to facilitate pre-binned hist plots #15019
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
Add step option where='edges' to facilitate pre-binned hist plots #15019
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a nice new feature! I've left a comment, and additionally
- I think having edges would be better
- This could do with a test, you could see if any existing tests in
lib/matplotlib/tests/test_axes.py
can be adapted to exercise the new option.
lib/matplotlib/axes/_axes.py
Outdated
@@ -2089,6 +2089,8 @@ def step(self, x, y, *args, where='pre', data=None, **kwargs): | |||
every *x* position, i.e. the interval ``[x[i], x[i+1])`` has the | |||
value ``y[i]``. | |||
- 'mid': Steps occur half-way between the *x* positions. | |||
- 'edges': Expects dim(x) = dim(y) + 1, steps have y[i] value on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think len(x)
instead of dim(x)
here?
Closes #5855. |
Yes, that's intentional based on the consensus in @6669. I would be ok with having both options, as we might want the above implementation to signify that we have no info about the rest of the distribution, but other might want the same results as with Naively I would expect |
As a reminder, there's also #5855 where I suggested allowing both len(x) == len(y)-1 (the case here) and len(y) == len(x)+1 (with "edges", or altenatively this can be seen as a histogram flipped by 90° -- indeed, my intent was to make it easy to switch between one orientation and the other). |
Tbh not sure how to implement that, but also I don't really like the idea of implicitly flipping x and y. |
It does not have to be implicit, but let's say I would prefer it not to be much harder to do a horizontal histogram rather than a vertical one. |
On that note, there seems to be some inconsistency on how
and
Either show or don't show the last edge. |
The issues with having the closing edges is that the only way to make it work seems if one could specify transform point by point. Right now it looks like it's possible to have x in data coords and y in axes coords, but I don't see how to do it for an line with coords [data, data] [data, axes]. If there's no better solution I'll make it default to a small nonzero number. |
you can (should be able to... no guarantees) be able to make it zero and the log-scale handler will clip it itself to a small nonzero number. |
I guess a variant would be to have orientation="horizontal"/"vertical" which is consistent with the rest of matplotlib (whereas flipxy exists nowhere else). Probably needs some discussion... |
I had that at first, but apart from being a handful to write I got confused which way was ''vertical" and which way was "horizontal". I don't have a strong feeling either way. Would be great to get a consensus, so I can start putting in tests. |
The orientation would be the same as for hist(). I agree it's not clear this API would be best, just suggesting it. |
@dstansby I've added tests, could you review and tell me what's missing? |
In general I like the idea, but I won't have the time to properly review this in the coming days at least. I'm not claiming exclusive rights to this PR either :) |
Although a quick thought is that after all, it may not be such a great idea to have an 'orientation' kwarg which only has an effect when 'where' has a certain value ("edges"), so perhaps instead having where="edges-horizontal"/"edges-vertical" (or some better name, but basically folding this into 'where') may be better. |
Well it works for both new symmetric where options. I didn't modify the rest of them because its not clear to me what the behavior of pre and post should look like. Tbh I'd be curious to know what fraction of step calls is not plotting histograms. |
At least when I first proposed the idea in #5855 the goal was not histograms (see #5855 (comment) and #5855 (comment)). |
Apart from the obvious I fail to see what's different about this comment and as implemented here As for this comment we're planning to make something at least for mpl-hep of the signature |
I left my problem with this implementation as a comment to #5855. |
f"{y.shape}") | ||
|
||
if orientation == 'horizontal': | ||
y = np.r_[y[0], y] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see #16142 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright., btw this PR is superseded by #15065, I should close it
See #15065 |
PR Summary
First attempt to address one of the requests raised in #6669 simplifying the api of step for pre-binned histogram plotting like
from
to
The consensus seems to have landed on no lines at the end, but this could easily be switched.

In this specific case I am also not sure whether 'edges' is the best keyword, maybe 'bins' would be better if we don't have the end lines go to -inf.
I'll be happy about feedback on the actual implementation.
Also it would be good to know if it's better to address the various enhancements in #6669 one by one or to combine things into less PRs.
PR Checklist