Skip to content

Fix wrong error message in #11919 #11930

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

Closed
wants to merge 4 commits into from
Closed
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
22 changes: 11 additions & 11 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4213,23 +4213,23 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
# NB: remember that a single color is also acceptable.
# Besides *colors* will be an empty array if c == 'none'.
valid_shape = False
raise ValueError
except ValueError:
if not valid_shape: # but at least one conversion succeeded.
raise ValueError(
"'c' argument has {nc} elements, which is not "
"acceptable for use with 'x' with size {xs}, "
"'y' with size {ys}."
.format(nc=n_elem, xs=x.size, ys=y.size)
)
# Both the mapping *and* the RGBA conversion failed: pretty
# severe failure => one may appreciate a verbose feedback.
raise ValueError(
"'c' argument must either be valid as mpl color(s) "
"or as numbers to be mapped to colors. "
"Here c = {}." # <- beware, could be long depending on c.
.format(c)
)
except Exception as e:
if valid_shape:
# Both the mapping *and* the RGBA conversion failed: pretty
# severe failure => one may appreciate a verbose feedback.
raise ValueError(
"'c' argument must either be valid as mpl color(s) "
"or as numbers to be mapped to colors. "
"Here c = {}." # <- beware, could be long depending on c.
.format(c)
) from e
raise e
else:
colors = None # use cmap, norm after collection is created

Expand Down
14 changes: 10 additions & 4 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,11 +1754,14 @@ def test_scatter_color(self):
# Value-mapping like
([0.5]*3, None), # should emit a warning for user's eyes though
([0.5]*4, None), # NB: no warning as matching size allows mapping
([0.5]*5, "shape"),
([0.5]*5, "wrong"),
(np.array([0.5]*5), "wrong"),
# RGB values
([[1, 0, 0]], None),
([[128, 101, 167]], "wrong"),
([[1, 0, 0]]*3, "shape"),
([[1, 0, 0]]*4, None),
([[128, 101, 167]]*4, "wrong"),
([[1, 0, 0]]*5, "shape"),
# RGBA values
([[1, 0, 0, 0.5]], None),
Expand All @@ -1780,16 +1783,19 @@ def test_scatter_color(self):
def test_scatter_c(self, c_case, re_key):
# Additional checking of *c* (introduced in #11383).
REGEXP = {
"shape": "^'c' argument has [0-9]+ elements", # shape mismatch
"conversion": "^'c' argument must either be valid", # bad vals
"shape": [ValueError, "^'c' argument has [0-9]+ elements"],
# shape mismatch
"conversion": [ValueError, "^'c' argument must either be valid"],
# bad vals
"wrong": [Exception, ".*"], # any exception
}
x = y = [0, 1, 2, 3]
fig, ax = plt.subplots()

if re_key is None:
ax.scatter(x, y, c=c_case, edgecolors="black")
else:
with pytest.raises(ValueError, match=REGEXP[re_key]):
with pytest.raises(REGEXP[re_key][0], match=REGEXP[re_key][1]):
ax.scatter(x, y, c=c_case, edgecolors="black")


Expand Down