Skip to content

Reverse legend - #24759

Merged
story645 merged 15 commits into
matplotlib:mainfrom
mkrake:reverse_legend
Dec 25, 2022
Merged

story645 merged 15 commits into
matplotlib:mainfrom
mkrake:reverse_legend

Conversation

@mkrake

@mkrake mkrake commented Dec 17, 2022

Copy link
Copy Markdown
Contributor

PR Summary

I added a parameter for the reverse legend option. See the following images. Taken from #24712.

without_reverse
with_reverse

PR Checklist

Documentation and Tests

  • [N/A] Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • [N/A] New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • [N/A]API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • [N/A] Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

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.

@timhoffm timhoffm left a comment •

Copy link
Copy Markdown
Member

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.

Comment thread lib/matplotlib/legend.py
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

Copy link
Copy Markdown
Member

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

Comment thread lib/matplotlib/legend.py Resolved
@tacaswell tacaswell added this to the v3.8.0 milestone Dec 18, 2022

@timhoffm timhoffm left a comment

Copy link
Copy Markdown
Member

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.

Comment thread lib/matplotlib/tests/test_legend.py Resolved
Comment thread lib/matplotlib/legend.py Resolved
@story645

Copy link
Copy Markdown
Member

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

@mkrake

mkrake commented Dec 20, 2022

Copy link
Copy Markdown
Contributor Author

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?

@story645

Copy link
Copy Markdown
Member

can you join incubator? someone can definitely help you there/ it'll probably be easier - ask at https://gitter.im/matplotlib/matplotlib to join

Comment thread lib/matplotlib/legend.py Resolved
Legend: docstring correct spelling

Co-authored-by: hannah <story645@gmail.com>

@story645 story645 left a comment

Copy link
Copy Markdown
Member

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?

@mkrake

mkrake commented Dec 22, 2022 •

Copy link
Copy Markdown
Contributor Author

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.

@tacaswell

Copy link
Copy Markdown
Member

This should definitely get a whats_new entry!

I think we can sneak this in for 3.7.

Comment thread lib/matplotlib/legend.py
Comment on lines +212 to +215
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.

@story645 story645 Dec 22, 2022 •

Copy link
Copy Markdown
Member

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

@ksunden ksunden modified the milestones: v3.8.0, v3.7.0 Dec 22, 2022
@timhoffm

timhoffm commented Dec 22, 2022 •

Copy link
Copy Markdown
Member

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

@story645 story645 left a comment

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Reversed order of legend entries
Reverse order of legend entries

Comment thread lib/matplotlib/legend.py
labels.reverse()
handles.reverse()

handles = list(handles)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
handles = list(handles)

@rcomer rcomer linked an issue Dec 23, 2022 that may be closed by this pull request
@story645
story645 merged commit edfd3b4 into matplotlib:main Dec 25, 2022
@story645

Copy link
Copy Markdown
Member

Sorry for the delay in merging, thanks for contributing this new feature, & congrats on your first merged PR @Marvvxi 🥳
We hope to see you again 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

[ENH]: Reverse legend

5 participants