diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 63b16c311810..2d3d5e16c164 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4303,13 +4303,16 @@ def invalid_shape_exception(csize, xsize): try: # Is 'c' acceptable as PathCollection facecolors? colors = mcolors.to_rgba_array(c) except (TypeError, ValueError) as err: - if not valid_shape: - raise invalid_shape_exception(c.size, xsize) from err - # Both the mapping *and* the RGBA conversion failed: pretty - # severe failure => one may appreciate a verbose feedback. - raise ValueError( - f"'c' argument must be a color, a sequence of colors, or " - f"a sequence of numbers, not {c}") from err + if "RGBA values should be within 0-1 range" in str(err): + raise + else: + if not valid_shape: + raise invalid_shape_exception(c.size, xsize) from err + # Both the mapping *and* the RGBA conversion failed: pretty + # severe failure => one may appreciate a verbose feedback. + raise ValueError( + f"'c' argument must be a color, a sequence of colors, " + f"or a sequence of numbers, not {c}") from err else: if len(colors) not in (0, 1, xsize): # NB: remember that a single color is also acceptable. diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 53bc10b15b3d..920e3dcae960 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1977,6 +1977,17 @@ def get_next_color(): assert result_edgecolors == expected_edgecolors +def test_parse_scatter_color_args_error(): + def get_next_color(): + return 'blue' # currently unused + + with pytest.raises(ValueError, + match="RGBA values should be within 0-1 range"): + c = np.array([[0.1, 0.2, 0.7], [0.2, 0.4, 1.4]]) # value > 1 + mpl.axes.Axes._parse_scatter_color_args( + c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color) + + def test_as_mpl_axes_api(): # tests the _as_mpl_axes api from matplotlib.projections.polar import PolarAxes