From 952ca40d0c17ee7ffb96a4b1cac1cd5d1969ac41 Mon Sep 17 00:00:00 2001 From: davidleejy Date: Fri, 24 Aug 2018 17:56:37 +0800 Subject: [PATCH 1/3] chained exceptions so that exceptions raised by mcolors.to_rgba_array() are not hidden. --- lib/matplotlib/axes/_axes.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ed7552418ae7..77311c959c43 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -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 From 5fca75e90cf10afc91a4c2a32164111a89b0206a Mon Sep 17 00:00:00 2001 From: davidleejy Date: Mon, 27 Aug 2018 23:38:28 +0800 Subject: [PATCH 2/3] Fix tests --- lib/matplotlib/tests/test_axes.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a5b7331d3777..44ac0c18a23c 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1754,7 +1754,8 @@ 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), ([[1, 0, 0]]*3, "shape"), @@ -1780,8 +1781,11 @@ 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() @@ -1789,7 +1793,7 @@ def test_scatter_c(self, c_case, re_key): 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") From 9806c1a540790a0f5f9ac44f45f0bb56e2f631cc Mon Sep 17 00:00:00 2001 From: davidleejy Date: Mon, 27 Aug 2018 23:53:24 +0800 Subject: [PATCH 3/3] add tests for color values 0 to 255 range --- lib/matplotlib/tests/test_axes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 44ac0c18a23c..3978d4ed000f 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1758,8 +1758,10 @@ def test_scatter_color(self): (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),