Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2612f55

Browse files
committed
Deprecate string as color sequence
1 parent c4382b0 commit 2612f55

4 files changed

Lines changed: 42 additions & 7 deletions

File tree

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+
# 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
@@ -1954,9 +1954,6 @@ def test_scatter_different_shapes(self, fig_test, fig_ref):
19541954
# single string:
19551955
('0.5', None),
19561956
# Single letter-sequences
1957-
("rgby", None),
1958-
("rgb", "shape"),
1959-
("rgbrgb", "shape"),
19601957
(["rgby"], "conversion"),
19611958
# Special cases
19621959
("red", None),
@@ -3506,7 +3503,6 @@ def test_eventplot_defaults():
35063503
('0.5',), # string color with multiple characters: not OK before #8193 fix
35073504
('tab:orange', 'tab:pink', 'tab:cyan', 'bLacK'), # case-insensitive
35083505
('red', (0, 1, 0), None, (1, 0, 1, 0.5)), # a tricky case mixing types
3509-
('rgbk',) # len('rgbk') == len(data) and each character is a valid color
35103506
])
35113507
def test_eventplot_colors(colors):
35123508
'''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
@@ -769,13 +769,32 @@ def test_conversions():
769769
hex_color
770770

771771

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

0 commit comments

Comments
 (0)