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

Skip to content

Commit fdb5214

Browse files
committed
Add a test for checking the displayed legend is reversed
1 parent 3723815 commit fdb5214

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/matplotlib/tests/test_legend.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ def test_legend_remove():
294294
assert ax.get_legend() is None
295295

296296

297-
def test_reverse_legend():
297+
def test_reverse_legend_handles_and_labels():
298+
"""Check that the legend handles and labels are reversed."""
298299
fig, ax = plt.subplots()
299300
x = 1
300301
y = 1
@@ -307,8 +308,22 @@ def test_reverse_legend():
307308
leg = ax.legend(reverse=True)
308309
actual_labels = [t.get_text() for t in leg.get_texts()]
309310
actual_markers = [h.get_marker() for h in leg.legend_handles]
310-
assert (actual_labels, actual_markers) == (list(reversed(labels)),
311-
(list(reversed(markers))))
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()
312327

313328

314329
class TestLegendFunction:

0 commit comments

Comments
 (0)