|
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
|
@@ -2909,6 +2910,33 @@ def test_eventplot_defaults():
|
2909 | 2910 | colls = axobj.eventplot(data)
|
2910 | 2911 |
|
2911 | 2912 |
|
| 2913 | +@pytest.mark.parametrize(('colors'), [ |
| 2914 | + ('0.5',), # string color with multiple characters: not OK before #8193 fix |
| 2915 | + ('tab:orange', 'tab:pink', 'tab:cyan', 'bLacK'), # case-insensitive |
| 2916 | + ('red', (0, 1, 0), None, (1, 0, 1, 0.5)), # a tricky case mixing types |
| 2917 | + ('rgbk',) # len('rgbk') == len(data) and each character is a valid color |
| 2918 | +]) |
| 2919 | +def test_eventplot_colors(colors): |
| 2920 | + '''Test the *colors* parameter of eventplot. Inspired by the issue #8193. |
| 2921 | + ''' |
| 2922 | + data = [[i] for i in range(4)] # 4 successive events of different nature |
| 2923 | + |
| 2924 | + # Build the list of the expected colors |
| 2925 | + expected = [c if c is not None else 'C0' for c in colors] |
| 2926 | + # Convert the list into an array of RGBA values |
| 2927 | + # NB: ['rgbk'] is not a valid argument for to_rgba_array, while 'rgbk' is. |
| 2928 | + if len(expected) == 1: |
| 2929 | + expected = expected[0] |
| 2930 | + expected = broadcast_to(mcolors.to_rgba_array(expected), (len(data), 4)) |
| 2931 | + |
| 2932 | + fig, ax = plt.subplots() |
| 2933 | + if len(colors) == 1: # tuple with a single string (like '0.5' or 'rgbk') |
| 2934 | + colors = colors[0] |
| 2935 | + collections = ax.eventplot(data, colors=colors) |
| 2936 | + |
| 2937 | + for coll, color in zip(collections, expected): |
| 2938 | + assert_allclose(coll.get_color(), color) |
| 2939 | + |
2912 | 2940 | @image_comparison(baseline_images=['test_eventplot_problem_kwargs'],
|
2913 | 2941 | extensions=['png'], remove_text=True)
|
2914 | 2942 | def test_eventplot_problem_kwargs():
|
|
0 commit comments