Skip to content

Commit 79c00e9

Browse files
afvincenttacaswell
authored andcommitted
Add a non-picture test dedicated to the 'colors' parameter
1 parent 78e2134 commit 79c00e9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import matplotlib.colors as mcolors
2828
from numpy.testing import assert_allclose, assert_array_equal
2929
from matplotlib.cbook import IgnoredKeywordWarning
30+
from matplotlib.cbook._backports import broadcast_to
3031

3132
# Note: Some test cases are run twice: once normally and once with labeled data
3233
# These two must be defined in the same test function or need to have
@@ -2985,6 +2986,33 @@ def test_eventplot_defaults():
29852986
colls = axobj.eventplot(data)
29862987

29872988

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+
29883016
@image_comparison(baseline_images=['test_eventplot_problem_kwargs'],
29893017
extensions=['png'], remove_text=True)
29903018
def test_eventplot_problem_kwargs():

0 commit comments

Comments
 (0)