-
-
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
Changes from all commits
a94ecc5
cda9334
cb52fee
75d9849
a67ee8a
5633cdf
8964e8a
b9f19e1
704184b
fad4e44
2a5ed70
74c5d44
3723815
fdb5214
15911a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
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 commentThe 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) |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -209,6 +209,12 @@ def _update_bbox_to_anchor(self, loc_in_canvas): | |||
If *True*, legend marker is placed to the left of the legend label. | ||||
If *False*, legend marker is placed to the right of the legend label. | ||||
|
||||
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. | ||||
|
||||
Comment on lines
+212
to
+215
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
https://matplotlib.org/devdocs/devel/coding_guide.html#new-features-and-api-changes |
||||
..versionadded:: 3.7 | ||||
|
||||
frameon : bool, default: :rc:`legend.frameon` | ||||
Whether the legend should be drawn on a patch (frame). | ||||
|
||||
|
@@ -312,6 +318,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 commentThe 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). |
||||
scatterpoints=None, # number of scatter points | ||||
scatteryoffsets=None, | ||||
prop=None, # properties for the legend texts | ||||
|
@@ -437,6 +444,10 @@ def val_or_rc(val, rc_name): | |||
_hand.append(handle) | ||||
labels, handles = _lab, _hand | ||||
|
||||
if reverse: | ||||
labels.reverse() | ||||
handles.reverse() | ||||
|
||||
story645 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
handles = list(handles) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
if len(handles) < 2: | ||||
ncols = 1 | ||||
|
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.