Skip to content

Commit fe66213

Browse files
committed
TST: add tests for get_standard_colors
1 parent 393ae46 commit fe66213

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

pandas/tests/plotting/test_style.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
from cycler import cycler
2+
import matplotlib as mpl
3+
import matplotlib.colors
4+
import pytest
5+
6+
from pandas import Series
7+
8+
from pandas.plotting._matplotlib.style import get_standard_colors
9+
10+
11+
class TestGetStandardColors:
12+
@pytest.mark.parametrize(
13+
"num_colors, expected",
14+
[
15+
(3, ["red", "green", "blue"]),
16+
(5, ["red", "green", "blue", "red", "green"]),
17+
(7, ["red", "green", "blue", "red", "green", "blue", "red"]),
18+
(2, ["red", "green"]),
19+
(1, ["red"]),
20+
],
21+
)
22+
def test_default_colors_named_from_prop_cycle(self, num_colors, expected):
23+
mpl_params = {
24+
"axes.prop_cycle": cycler(color=["red", "green", "blue"]),
25+
}
26+
with mpl.rc_context(rc=mpl_params):
27+
result = get_standard_colors(num_colors=num_colors)
28+
assert result == expected
29+
30+
@pytest.mark.parametrize(
31+
"num_colors, expected",
32+
[
33+
(1, ["b"]),
34+
(3, ["b", "g", "r"]),
35+
(4, ["b", "g", "r", "y"]),
36+
(5, ["b", "g", "r", "y", "b"]),
37+
(7, ["b", "g", "r", "y", "b", "g", "r"]),
38+
],
39+
)
40+
def test_default_colors_named_from_prop_cycle_string(self, num_colors, expected):
41+
mpl_params = {
42+
"axes.prop_cycle": cycler(color="bgry"),
43+
}
44+
with mpl.rc_context(rc=mpl_params):
45+
result = get_standard_colors(num_colors=num_colors)
46+
assert result == expected
47+
48+
@pytest.mark.parametrize(
49+
"num_colors, expected_name",
50+
[
51+
(1, ["C0"]),
52+
(3, ["C0", "C1", "C2"]),
53+
(
54+
12,
55+
[
56+
"C0",
57+
"C1",
58+
"C2",
59+
"C3",
60+
"C4",
61+
"C5",
62+
"C6",
63+
"C7",
64+
"C8",
65+
"C9",
66+
"C0",
67+
"C1",
68+
],
69+
),
70+
],
71+
)
72+
def test_default_colors_named_undefined_prop_cycle(self, num_colors, expected_name):
73+
with mpl.rc_context(rc={}):
74+
expected = [matplotlib.colors.to_hex(x) for x in expected_name]
75+
result = get_standard_colors(num_colors=num_colors)
76+
assert result == expected
77+
78+
@pytest.mark.parametrize(
79+
"num_colors, expected",
80+
[
81+
(1, ["red", "green", (0.1, 0.2, 0.3)]),
82+
(2, ["red", "green", (0.1, 0.2, 0.3)]),
83+
(3, ["red", "green", (0.1, 0.2, 0.3)]),
84+
(4, ["red", "green", (0.1, 0.2, 0.3), "red"]),
85+
],
86+
)
87+
def test_user_input_color_sequence(self, num_colors, expected):
88+
color = ["red", "green", (0.1, 0.2, 0.3)]
89+
result = get_standard_colors(color=color, num_colors=num_colors)
90+
assert result == expected
91+
92+
@pytest.mark.parametrize(
93+
"num_colors, expected",
94+
[
95+
(1, ["r", "g", "b", "k"]),
96+
(2, ["r", "g", "b", "k"]),
97+
(3, ["r", "g", "b", "k"]),
98+
(4, ["r", "g", "b", "k"]),
99+
(5, ["r", "g", "b", "k", "r"]),
100+
(6, ["r", "g", "b", "k", "r", "g"]),
101+
],
102+
)
103+
def test_user_input_color_string(self, num_colors, expected):
104+
color = "rgbk"
105+
result = get_standard_colors(color=color, num_colors=num_colors)
106+
assert result == expected
107+
108+
@pytest.mark.parametrize(
109+
"num_colors, expected",
110+
[
111+
(1, [(0.1, 0.2, 0.3)]),
112+
(2, [(0.1, 0.2, 0.3), (0.1, 0.2, 0.3)]),
113+
(3, [(0.1, 0.2, 0.3), (0.1, 0.2, 0.3), (0.1, 0.2, 0.3)]),
114+
],
115+
)
116+
def test_user_input_color_floats(self, num_colors, expected):
117+
color = (0.1, 0.2, 0.3)
118+
result = get_standard_colors(color=color, num_colors=num_colors)
119+
assert result == expected
120+
121+
@pytest.mark.parametrize(
122+
"color, num_colors, expected",
123+
[
124+
("Crimson", 1, ["Crimson"]),
125+
("DodgerBlue", 2, ["DodgerBlue", "DodgerBlue"]),
126+
("firebrick", 3, ["firebrick", "firebrick", "firebrick"]),
127+
],
128+
)
129+
def test_user_input_named_color_string(self, color, num_colors, expected):
130+
result = get_standard_colors(color=color, num_colors=num_colors)
131+
assert result == expected
132+
133+
@pytest.mark.parametrize("color", ["", [], (), Series([], dtype="object")])
134+
def test_empty_color_raises(self, color):
135+
with pytest.raises(ValueError, match="Invalid color argument"):
136+
get_standard_colors(color=color, num_colors=1)
137+
138+
@pytest.mark.parametrize(
139+
"color",
140+
[
141+
"BAD_COLOR",
142+
("red", "green", "BAD_COLOR"),
143+
(0.1,),
144+
(0.1, 0.2),
145+
(0.1, 0.2, 0.3, 0.4, 0.5), # must be either 3 or 4 floats
146+
],
147+
)
148+
def test_bad_color_raises(self, color):
149+
with pytest.raises(ValueError, match="Invalid color"):
150+
get_standard_colors(color=color, num_colors=5)

0 commit comments

Comments
 (0)