Skip to content

Improve error handling in _parse_scatter_color_args #17245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down