-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Reverse legend #24759
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
Reverse legend #24759
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.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a while, please feel free to ping @matplotlib/developers
or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join us on gitter for real-time discussion.
For details on testing, writing docs, and our review process, please see the developer guide
We strive to be a welcoming and open project. Please follow our Code of Conduct.
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.
Thanks for the contribution!
Please add a test.
@@ -312,6 +312,7 @@ def __init__( | |||
numpoints=None, # number of points in the legend line | |||
markerscale=None, # relative size of legend markers vs. original | |||
markerfirst=True, # left/right ordering of legend marker and label | |||
reverse=False, # reverse ordering of legend marker and label |
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 add a docstring as well. This should go in line 212 (we do a bit of docstring processing that is why the docstring text is in a separate place).
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.
Thanks, this is very much in the right direction. Only one extension to the test left.
I think you accidentally pulled the main commits into your branch instead of rebasing on main. If you need help cleaning up your branch, let us know and check out https://matplotlib.org/devdocs/devel/development_workflow.html#recovering-from-mess-ups (and let us know if anything in that doc is confusing!) |
Ups, sorry for the chaos. I tried something, but now my comments in my local repository behind the main comments. If I try to push, I get the message everything up-to-date. Can you help my to clean up my branch, please? |
can you join incubator? someone can definitely help you there/ it'll probably be easier - ask at https://gitter.im/matplotlib/matplotlib to join |
80a8cd0
to
1a00ed4
Compare
Legend: Refactoring actual labels Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1a00ed4
to
2a5ed70
Compare
Legend: docstring correct spelling Co-authored-by: hannah <story645@gmail.com>
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.
Thanks for implementing this! It needs a second approval to merge, but at that point would you like to try rebasing again (w/ help from folks in incubator) or would you like us to squash merge?
Thanks for approving and introduction into the development with matplotlib. I would say that the squash merge option is the better way. There are some small not so important commits. |
This should definitely get a whats_new entry! I think we can sneak this in for 3.7. |
reverse : bool, default: False | ||
If *True*, the legend labels are displayed in reverse order from the input. | ||
If *False*, the legend labels are displayed in the same order as the input. | ||
|
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.
Forgot, but based on @tacaswell comment this should also get a version added at the end (skip a line) so like:
..versionadded:: 3.7
https://matplotlib.org/devdocs/devel/coding_guide.html#new-features-and-api-changes
4e8fe33
to
f6012b4
Compare
f6012b4
to
15911a6
Compare
Thanks @Marvvxi! To keep things simple for you and not go through additional iterations, I've done the final polishing and added a "What's new" entry as well as an additional figure comparison test that the displayed order is indeed reversed. Feel free to check the additions. @story645 please re-review the additions, and squash-merge when ok. Since my additions are only documetation and a test, I figure we can merge without a third reviewer (I as a the second reviewer have contributed, but only non-code parts). |
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.
Take or leave nits - I'll give a couple hours for @Marvvxi to comment & merge tomorrow afternoon EST if I don't hear back.
Reversed order of legend entries | ||
-------------------------------- | ||
The order of legend entries can now be reversed by passing ``reverse=True`` to | ||
`~.Axes.legend`. |
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.
Not sure if it's overkill to have a teeny example here, something like
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(4,2))
for ax in [ax1, ax2]:
ax.axhline(.25, label="25")
ax.axhline(.75, label="75")
ax1.legend()
ax2.legend(reverse=True)
@@ -0,0 +1,4 @@ | |||
Reversed order of legend entries |
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.
Reversed order of legend entries | |
Reverse order of legend entries |
if reverse: | ||
labels.reverse() | ||
handles.reverse() | ||
|
||
handles = list(handles) |
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.
handles = list(handles) |
Sorry for the delay in merging, thanks for contributing this new feature, & congrats on your first merged PR @Marvvxi 🥳 |
PR Summary
I added a parameter for the reverse legend option. See the following images. Taken from #24712.
PR Checklist
Documentation and Tests
pytest
passes)Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst