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

Skip to content

Commit 69edfa2

Browse files
authored
Merge pull request #19571 from anntzer/ccl
Fail early when setting Text color to a non-colorlike.
2 parents 1ec609a + 8cde5a8 commit 69edfa2

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

lib/matplotlib/colors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ def is_color_like(c):
147147
return True
148148

149149

150+
def _check_color_like(**kwargs):
151+
"""
152+
For each *key, value* pair in *kwargs*, check that *value* is color-like.
153+
"""
154+
for k, v in kwargs.items():
155+
if not is_color_like(v):
156+
raise ValueError(f"{v!r} is not a valid value for {k}")
157+
158+
150159
def same_color(c1, c2):
151160
"""
152161
Return whether the colors *c1* and *c2* are the same.

lib/matplotlib/lines.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from .artist import Artist, allow_rasterization
1414
from .cbook import (
1515
_to_unmasked_float_array, ls_mapper, ls_mapper_r, STEP_LOOKUP_MAP)
16-
from .colors import is_color_like, get_named_colors_mapping
1716
from .markers import MarkerStyle
1817
from .path import Path
1918
from .transforms import Bbox, BboxTransformTo, TransformedPath
@@ -1050,9 +1049,8 @@ def set_color(self, color):
10501049
----------
10511050
color : color
10521051
"""
1053-
if not is_color_like(color) and color != 'auto':
1054-
_api.check_in_list(get_named_colors_mapping(),
1055-
_print_supported_values=False, color=color)
1052+
if not cbook._str_equal(color, 'auto'):
1053+
mcolors._check_color_like(color=color)
10561054
self._color = color
10571055
self.stale = True
10581056

lib/matplotlib/tests/test_text.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,8 @@ def test_update_mutate_input():
709709
t.update(inp)
710710
assert inp['fontproperties'] == cache['fontproperties']
711711
assert inp['bbox'] == cache['bbox']
712+
713+
714+
def test_invalid_color():
715+
with pytest.raises(ValueError):
716+
plt.figtext(.5, .5, "foo", c="foobar")

lib/matplotlib/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,10 @@ def set_color(self, color):
934934
----------
935935
color : color
936936
"""
937+
# "auto" is only supported by axisartist, but we can just let it error
938+
# out at draw time for simplicity.
939+
if not cbook._str_equal(color, "auto"):
940+
mpl.colors._check_color_like(color=color)
937941
# Make sure it is hashable, or get_prop_tup will fail.
938942
try:
939943
hash(color)

0 commit comments

Comments
 (0)