|
7 | 7 | import matplotlib as mpl
|
8 | 8 | from matplotlib.testing.decorators import image_comparison
|
9 | 9 | from matplotlib import pyplot as plt, rc_context
|
10 |
| -from matplotlib.colors import LogNorm |
| 10 | +from matplotlib.colors import LogNorm, same_color |
11 | 11 | import pytest
|
12 | 12 |
|
13 | 13 |
|
@@ -478,3 +478,43 @@ def test_contour_autolabel_beyond_powerlimits():
|
478 | 478 | ax.clabel(cs)
|
479 | 479 | # Currently, the exponent is missing, but that may be fixed in the future.
|
480 | 480 | 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