8000 Added tests for ContourSet.legend_elements · matplotlib/matplotlib@88129e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88129e7

Browse files
committed
Added tests for ContourSet.legend_elements
1 parent 075067c commit 88129e7

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

lib/matplotlib/tests/test_contour.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import matplotlib as mpl
88
from matplotlib.testing.decorators import image_comparison
99
from matplotlib import pyplot as plt, rc_context
10-
from matplotlib.colors import LogNorm
10+
from matplotlib.colors import LogNorm, same_color
1111
import pytest
1212

1313

@@ -478,3 +478,43 @@ def test_contour_autolabel_beyond_powerlimits():
478478
ax.clabel(cs)
479479
# Currently, the exponent is missing, but that may be fixed in the future.
480480
assert {text.get_text() for text in ax.texts} == {"0.25", "1.00", "4.00"}
481+
482+
483+
def test_contourf_legend_elements():
484+
from matplotlib.patches import Rectangle
485+
x = np.arange(1, 10)
486+
y = x.reshape(-1, 1)
487+
h = x * y
488+
489+
cs = plt.contourf(h, levels=[10, 30, 50],
490+
colors=['#FFFF00', '#FF00FF', '#00FFFF'],
491+
extend='both')
492+
cs.cmap.set_over('red')
493+
cs.cmap.set_under('blue')
494+
cs.changed()
495+
artists, labels = cs.legend_elements()
496+
assert labels == ['$x \\leq -1e+250s$',
497+
'$10.0 < x \\leq 30.0$',
498+
'$30.0 < x \\leq 50.0$',
499+
'$x > 1e+250s$']
500+
expected_colors = ('blue', '#FFFF00', '#FF00FF', 'red')
501+
assert all(isinstance(a, Rectangle) for a in artists)
502+
assert all(same_color(a.get_facecolor(), c)
503+
for a, c in zip(artists, expected_colors))
504+
505+
506+
def test_contour_legend_elements():
507+
from matplotlib.collections import LineCollection
508+
x = np.arange(1, 10)
509+
y = x.reshape(-1, 1)
510+
h = x * y
511+
512+
colors = ['blue', '#00FF00', 'red']
513+
cs = plt.contour(h, levels=[10, 30, 50],
514+
colors=colors,
515+
extend='both')
516+
artists, labels = cs.legend_elements()
517+
assert labels == ['$x = 10.0$', '$x = 30.0$', '$x = 50.0$']
518+
assert all(isinstance(a, LineCollection) for a in artists)
519+
assert all(same_color(a.get_color(), c)
520+
for a, c in zip(artists, colors))

0 commit comments

Comments
 (0)
0