-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
hist reverses legend order if histtype is not bar #8461
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
Comments
The culprit is the comment at the top of
|
I think any reasonable fix to this will lead to inversing the order in which the bars are being drawn (which is fine with me -- you'd expect the later datasets to be plotted on top of the earlier ones per standard matplotlib behavior -- but would definitely cause a backcompatibility break). |
This bug is still present in matplotlib 3.7.1 and python 3.11.2. For the reason given by @anntzer above, it becomes even more confusing if one calls x1 = np.random.randn(100)
x2 = np.random.rand(100)
plt.hist([x1, x2], histtype='step')
plt.legend(['x1', 'x2']) yields reversed colours and labels: It's easy to see how this bug could fly under the radar: the swapping of colours and labels in code for a work project this morning confused me for quite some time. |
I believe this is partially addressed by #24759, adjusting @efiring 's example to use The example from @davidjamesweir is a less than great feature of the API where you can pass |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
The reversal dates back to 2e1a96d, but I see no obvious reason why that has been done. One could retain the draw order while reversing the lists by adding minimal zorder (e.g. 1e-12). But that's a bit hacky, and also the draw order being different between types bar and stepfilled is quite surprising. I would consider this buggy behavior even though it seems to have been introduced intentionally. In that sense I'd be fine with just reversing the order and calling it a bug fix. |
Time to fix this bug. I guess that most existing code is no longer in use. Suggestion: Let the parameter |
That migration pattern does not really help much: We've introduced The slightly better approach would be to introduce a |
Matplotlib v2.0.x and master on Python 3.5:
Actual outcome

Expected outcome
With
histtype='bar'
the order of the variables is preserved, as I would expect, instead of reversed, as withstep
.The text was updated successfully, but these errors were encountered: