|
27 | 27 | import matplotlib.colors as mcolors
|
28 | 28 | from numpy.testing import assert_allclose, assert_array_equal
|
29 | 29 | from matplotlib.cbook import IgnoredKeywordWarning
|
| 30 | +from matplotlib.cbook._backports import broadcast_to |
30 | 31 |
|
31 | 32 | # Note: Some test cases are run twice: once normally and once with labeled data
|
32 | 33 | # These two must be defined in the same test function or need to have
|
@@ -2985,6 +2986,33 @@ def test_eventplot_defaults():
|
2985 | 2986 | colls = axobj.eventplot(data)
|
2986 | 2987 |
|
2987 | 2988 |
|
| 2989 | +@pytest.mark.parametrize(('colors'), [ |
| 2990 | + ('0.5',), # string color with multiple characters: not OK before #8193 fix |
| 2991 | + ('tab:orange', 'tab:pink', 'tab:cyan', 'bLacK'), # case-insensitive |
| 2992 | + ('red', (0, 1, 0), None, (1, 0, 1, 0.5)), # a tricky case mixing types |
| 2993 | + ('rgbk',) # len('rgbk') == len(data) and each character is a valid color |
| 2994 | +]) |
| 2995 | +def test_eventplot_colors(colors): |
| 2996 | + '''Test the *colors* parameter of eventplot. Inspired by the issue #8193. |
| 2997 | + ''' |
| 2998 | + data = [[i] for i in range(4)] # 4 successive events of different nature |
| 2999 | + |
| 3000 | + # Build the list of the expected colors |
| 3001 | + expected = [c if c is not None else 'C0' for c in colors] |
| 3002 | + # Convert the list into an array of RGBA values |
| 3003 | + # NB: ['rgbk'] is not a valid argument for to_rgba_array, while 'rgbk' is. |
| 3004 | + if len(expected) == 1: |
| 3005 | + expected = expected[0] |
| 3006 | + expected = broadcast_to(mcolors.to_rgba_array(expected), (len(data), 4)) |
| 3007 | + |
| 3008 | + fig, ax = plt.subplots() |
| 3009 | + if len(colors) == 1: # tuple with a single string (like '0.5' or 'rgbk') |
| 3010 | + colors = colors[0] |
| 3011 | + collections = ax.eventplot(data, colors=colors) |
| 3012 | + |
| 3013 | + for coll, color in zip(collections, expected): |
| 3014 | + assert_allclose(coll.get_color(), color) |
| 3015 | + |
2988 | 3016 | @image_comparison(baseline_images=['test_eventplot_problem_kwargs'],
|
2989 | 3017 | extensions=['png'], remove_text=True)
|
2990 | 3018 | def test_eventplot_problem_kwargs():
|
|
0 commit comments