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

Skip to content

Commit be3e477

Browse files
authored
Merge pull request #17245 from timhoffm/scatter-color-error-message
Improve error handling in _parse_scatter_color_args
2 parents e8c7579 + 0c44be6 commit be3e477

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,13 +4287,16 @@ def invalid_shape_exception(csize, xsize):
42874287
try: # Is 'c' acceptable as PathCollection facecolors?
42884288
colors = mcolors.to_rgba_array(c)
42894289
except (TypeError, ValueError) as err:
4290-
if not valid_shape:
4291-
raise invalid_shape_exception(c.size, xsize) from err
4292-
# Both the mapping *and* the RGBA conversion failed: pretty
4293-
# severe failure => one may appreciate a verbose feedback.
4294-
raise ValueError(
4295-
f"'c' argument must be a color, a sequence of colors, or "
4296-
f"a sequence of numbers, not {c}") from err
4290+
if "RGBA values should be within 0-1 range" in str(err):
4291+
raise
4292+
else:
4293+
if not valid_shape:
4294+
raise invalid_shape_exception(c.size, xsize) from err
4295+
# Both the mapping *and* the RGBA conversion failed: pretty
4296+
# severe failure => one may appreciate a verbose feedback.
4297+
raise ValueError(
4298+
f"'c' argument must be a color, a sequence of colors, "
4299+
f"or a sequence of numbers, not {c}") from err
42974300
else:
42984301
if len(colors) not in (0, 1, xsize):
42994302
# NB: remember that a single color is also acceptable.

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,17 @@ def get_next_color():
19761976
assert result_edgecolors == expected_edgecolors
19771977

19781978

1979+
def test_parse_scatter_color_args_error():
1980+
def get_next_color():
1981+
return 'blue' # currently unused
1982+
1983+
with pytest.raises(ValueError,
1984+
match="RGBA values should be within 0-1 range"):
1985+
c = np.array([[0.1, 0.2, 0.7], [0.2, 0.4, 1.4]]) # value > 1
1986+
mpl.axes.Axes._parse_scatter_color_args(
1987+
c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color)
1988+
1989+
19791990
def test_as_mpl_axes_api():
19801991
# tests the _as_mpl_axes api
19811992
from matplotlib.projections.polar import PolarAxes

0 commit comments

Comments
 (0)