Skip to content

Commit 0c44be6

Browse files
committed
Improve error handling in _parse_scatter_color_args
1 parent b94812c commit 0c44be6

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
@@ -4303,13 +4303,16 @@ def invalid_shape_exception(csize, xsize):
43034303
try: # Is 'c' acceptable as PathCollection facecolors?
43044304
colors = mcolors.to_rgba_array(c)
43054305
except (TypeError, ValueError) as err:
4306-
if not valid_shape:
4307-
raise invalid_shape_exception(c.size, xsize) from err
4308-
# Both the mapping *and* the RGBA conversion failed: pretty
4309-
# severe failure => one may appreciate a verbose feedback.
4310-
raise ValueError(
4311-
f"'c' argument must be a color, a sequence of colors, or "
4312-
f"a sequence of numbers, not {c}") from err
4306+
if "RGBA values should be within 0-1 range" in str(err):
4307+
raise
4308+
else:
4309+
if not valid_shape:
4310+
raise invalid_shape_exception(c.size, xsize) from err
4311+
# Both the mapping *and* the RGBA conversion failed: pretty
4312+
# severe failure => one may appreciate a verbose feedback.
4313+
raise ValueError(
4314+
f"'c' argument must be a color, a sequence of colors, "
4315+
f"or a sequence of numbers, not {c}") from err
43134316
else:
43144317
if len(colors) not in (0, 1, xsize):
43154318
# 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
@@ -1977,6 +1977,17 @@ def get_next_color():
19771977
assert result_edgecolors == expected_edgecolors
19781978

19791979

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

0 commit comments

Comments
 (0)