8000 Merge pull request #14259 from timhoffm/deprecate-str-as-colorsequence · matplotlib/matplotlib@a2b1482 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a2b1482

Browse files
authored
Merge pull request #14259 from timhoffm/deprecate-str-as-colorsequence
Deprecate string as color sequence
2 parents a7b8e3b + 2612f55 commit a2b1482

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Deprecations
2+
````````````
3+
4+
Using a string of single-character colors as a color sequence (e.g. "rgb") is
5+
deprecated. Use an explicit list instead.

lib/matplotlib/colors.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,25 @@ def to_rgba_array(c, alpha=None):
286286
return np.array([to_rgba(c, alpha)], float)
287287
except (ValueError, TypeError):
288288
pass
289+
289290
# Convert one at a time.
290-
result = np.empty((len(c), 4), float)
291-
for i, cc in enumerate(c):
292-
result[i] = to_rgba(cc, alpha)
291+
if isinstance(c, str):
292+
# Single string as color sequence.
293+ 10000
# This is deprecated and will be removed in the future.
294+
try:
295+
result = np.array([to_rgba(cc, alpha) for cc in c])
296+
except ValueError:
297+
raise ValueError(
298+
"'%s' is neither a valid single color nor a color sequence "
299+
"consisting of single character color specifiers such as "
300+
"'rgb'. Note also that the latter is deprecated." % c)
301+
else:
302+
cbook.warn_deprecated("3.2", message="Using a string of single "
303+
"character colors as a color sequence is "
304+
"deprecated. Use an explicit list instead.")
305+
else:
306+
result = np.array([to_rgba(cc, alpha) for cc in c])
307+
293308
return result
294309

295310

lib/matplotlib/tests/test_axes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,9 +1910,6 @@ def test_scatter_different_shapes(self, fig_test, fig_ref):
19101910
# single string:
19111911
('0.5', None),
19121912
# Single letter-sequences
1913-
("rgby", None),
1914-
("rgb", "shape"),
1915-
("rgbrgb", "shape"),
19161913
(["rgby"], "conversion"),
19171914
# Special cases
19181915
("red", None),
@@ -3424,7 +3421,6 @@ def test_eventplot_defaults():
34243421
('0.5',), # string color with multiple characters: not OK before #8193 fix
34253422
('tab:orange', 'tab:pink', 'tab:cyan', 'bLacK'), # case-insensitive
34263423
('red', (0, 1, 0), None, (1, 0, 1, 0.5)), # a tricky case mixing types
3427-
('rgbk',) # len('rgbk') == len(data) and each character is a valid color
34283424
])
34293425
def test_eventplot_colors(colors):
34303426
'''Test the *colors* parameter of eventplot. Inspired by the issue #8193.

lib/matplotlib/tests/test_colors.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,13 +767,32 @@ def test_conversions():
767767
hex_color
768768

769769

770+
def test_to_rgba_array_single_str():
771+
# single color name is valid
772+
assert_array_equal(mcolors.to_rgba_array("red"), [(1, 0, 0, 1)])
773+
774+
# single char color sequence is deprecated
775+
with pytest.warns(cbook.MatplotlibDeprecationWarning,
776+
match="Using a string of single character colors as a "
777+
"color sequence is deprecated"):
778+
array = mcolors.to_rgba_array("rgb")
779+
assert_array_equal(array, [(1, 0, 0, 1), (0, 0.5, 0, 1), (0, 0, 1, 1)])
780+
781+
with pytest.raises(ValueError,
782+
match="neither a valid single color nor a color "
783+
"sequence"):
784+
mcolors.to_rgba_array("rgbx")
785+
786+
770787
def test_failed_conversions():
771788
with pytest.raises(ValueError):
772789
mcolors.to_rgba('5')
773790
with pytest.raises(ValueError):
774791
mcolors.to_rgba('-1')
775792
with pytest.raises(ValueError):
776793
mcolors.to_rgba('nan')
794+
with pytest.raises(ValueError):
795+
mcolors.to_rgba('unknown_color')
777796
with pytest.raises(ValueError):
778797
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.
779798
mcolors.to_rgba(0.4)

0 commit comments

Comments
 (0)
0