Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edfd3b4

Browse files
story645timhoffm
andauthored
Merge pull request #24759 from Marvvxi/reverse_legend
Legend: added *reverse* parameter that reverses order of labels and handles in legend and tests for *reverse* parameter added what's new entry for legend(..., reverse=True), test for checking the displayed legend is reversed, and .. versionadded tag to reverse parameter Co-authored-by: Tim Hoffmann <[email protected]>
2 parents b4474c4 + 15911a6 commit edfd3b4

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Reversed order of legend entries
2+
--------------------------------
3+
The order of legend entries can now be reversed by passing ``reverse=True`` to
4+
`~.Axes.legend`.

lib/matplotlib/legend.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ def _update_bbox_to_anchor(self, loc_in_canvas):
209209
If *True*, legend marker is placed to the left of the legend label.
210210
If *False*, legend marker is placed to the right of the legend label.
211211
212+
reverse : bool, default: False
213+
If *True*, the legend labels are displayed in reverse order from the input.
214+
If *False*, the legend labels are displayed in the same order as the input.
215+
216+
..versionadded:: 3.7
217+
212218
frameon : bool, default: :rc:`legend.frameon`
213219
Whether the legend should be drawn on a patch (frame).
214220
@@ -312,6 +318,7 @@ def __init__(
312318
numpoints=None, # number of points in the legend line
313319
markerscale=None, # relative size of legend markers vs. original
314320
markerfirst=True, # left/right ordering of legend marker and label
321+
reverse=False, # reverse ordering of legend marker and label
315322
scatterpoints=None, # number of scatter points
316323
scatteryoffsets=None,
317324
prop=None, # properties for the legend texts
@@ -437,6 +444,10 @@ def val_or_rc(val, rc_name):
437444
_hand.append(handle)
438445
labels, handles = _lab, _hand
439446

447+
if reverse:
448+
labels.reverse()
449+
handles.reverse()
450+
440451
handles = list(handles)
441452
if len(handles) < 2:
442453
ncols = 1

lib/matplotlib/tests/test_legend.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,38 @@ def test_legend_remove():
294294
assert ax.get_legend() is None
295295

296296

297+
def test_reverse_legend_handles_and_labels():
298+
"""Check that the legend handles and labels are reversed."""
299+
fig, ax = plt.subplots()
300+
x = 1
301+
y = 1
302+
labels = ["First label", "Second label", "Third label"]
303+
markers = ['.', ',', 'o']
304+
305+
ax.plot(x, y, markers[0], label=labels[0])
306+
ax.plot(x, y, markers[1], label=labels[1])
307+
ax.plot(x, y, markers[2], label=labels[2])
308+
leg = ax.legend(reverse=True)
309+
actual_labels = [t.get_text() for t in leg.get_texts()]
310+
actual_markers = [h.get_marker() for h in leg.legend_handles]
311+
assert actual_labels == list(reversed(labels))
312+
assert actual_markers == list(reversed(markers))
313+
314+
315+
@check_figures_equal(extensions=["png"])
316+
def test_reverse_legend_display(fig_test, fig_ref):
317+
"""Check that the rendered legend entries are reversed"""
318+
ax = fig_test.subplots()
319+
ax.plot([1], 'ro', label="first")
320+
ax.plot([2], 'bx', label="second")
321+
ax.legend(reverse=True)
322+
323+
ax = fig_ref.subplots()
324+
ax.plot([2], 'bx', label="second")
325+
ax.plot([1], 'ro', label="first")
326+
ax.legend()
327+
328+
297329
class TestLegendFunction:
298330
# Tests the legend function on the Axes and pyplot.
299331
def test_legend_no_args(self):

0 commit comments

Comments
 (0)