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

Conversation

davidleejy
Copy link

@davidleejy davidleejy commented Aug 24, 2018

PR Summary

Fixes issue #11919 .

This PR chains (appropriate) exceptions raised by mcolors.to_rgba_array() to additional exceptions raised by its caller scatter(). Now, the exception raised by mcolors.to_rgba_array() is visible if color values are outside of 0 to 1 range.

Prior to this PR, exceptions are not chained - see commit e3a8004. Exceptions raised by mcolors.to_rgba_array() were suppressed by exceptions of its caller scatter().

This PR applies exception chaining solely on scatter function. Author is open to helping chain other exceptions.

PEP 3134 advises on Exception Chaining. Example use: https://stackoverflow.com/questions/16414744/python-exception-chaining

Outcome

Case 1: color values outside 0 to 1 range

python -c "import numpy as np; import matplotlib.pyplot as plt; f =plt.figure(); ax = plt.axes(); ax.scatter(np.array([2]), np.array([3]), c=np.array([[12,130,140]]))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/__init__.py", line 1764, in inner
    return func(ax, *args, **kwargs)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/axes/_axes.py", line 4232, in scatter
    raise e
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/axes/_axes.py", line 4210, in scatter
    colors = mcolors.to_rgba_array(c)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/colors.py", line 244, in to_rgba_array
    raise ValueError("RGBA values should be within 0-1 range")
ValueError: RGBA values should be within 0-1 range

Case 2: color array is wrong shape

python -c "import numpy as np; import matplotlib.pyplot as plt; f =plt.figure(); ax = plt.axes(); ax.scatter(np.array([2]), np.array([3]), c=np.array([[.12,.130,.140],[.1,.1,.1]]))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/__init__.py", line 1764, in inner
    return func(ax, *args, **kwargs)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/axes/_axes.py", line 4232, in scatter
    raise e
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/axes/_axes.py", line 4220, in scatter
    .format(nc=n_elem, xs=x.size, ys=y.size)
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 1, 'y' with size 1.

Case 3: color is 'jaune'

python -c "import numpy as np; import matplotlib.pyplot as plt; f =plt.figure(); ax = plt.axes(); ax.scatter(np.array([2]), np.array([3]), c='jaune')"
Traceback (most recent call last):
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/colors.py", line 158, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
KeyError: ('j', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/axes/_axes.py", line 4210, in scatter
    colors = mcolors.to_rgba_array(c)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/colors.py", line 259, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/colors.py", line 160, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/colors.py", line 204, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 'j'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/__init__.py", line 1764, in inner
    return func(ax, *args, **kwargs)
  File "/home/d/code/dljy-matplotlib/lib/matplotlib/axes/_axes.py", line 4231, in scatter
    ) from e
ValueError: 'c' argument must either be valid as mpl color(s) or as numbers to be mapped to colors. Here c = jaune.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak
Copy link
Member

jklymak commented Aug 24, 2018

Seems reasonable, but I'm not sure about the test failure.

@timhoffm
Copy link
Member

The test case for c=[0.5]*5 in test_scatter_c(test_axes.py l. 1780) must be adapted.

You have to add a new entry in REGEXP for the exception text you are generating in that case.

@davidleejy davidleejy changed the title Chained an appropriate exception in mcolors.to_rgba_array function (called in try...except block). Fix wrong error message in #11919 Aug 27, 2018
@davidleejy
Copy link
Author

davidleejy commented Aug 27, 2018

Appreciate the advice. Amended failing test.

Reasoning behind amendment: exceptions raised by called functions should be tested at the level of these called functions. For e.g., if an exception is expected from mcolors.to_rgba_array(), then tests for to_rgba_array() would cover these exceptions.

scatter() should raise exceptions that is in its scope to detect - e.g. passed 5 points to plot but only passed 2 colors in color array - resulting in shape mismatch.

Feel free to comment @afvincent if you're free :)

@anntzer
Copy link
Contributor

anntzer commented Mar 25, 2019

Sorry this fell through the cracks. This needs a rebase, but I think the basic idea, at least, is good.

@joseortiz3
Copy link

joseortiz3 commented Jun 24, 2019

Hello. I'm probably the thousandth person to see this nonsense error message:

ValueError: 'c' argument has 10 elements, which is not acceptable for use with 'x' with size 10, 'y' with size 10.

Please support the PR! (sorry I can't right now)

@anntzer
Copy link
Contributor

anntzer commented Jun 24, 2019

This was fixed in #13959.
As for the original PR author's cases, 2) and 3) have also been fixed in master, and see #11919 (comment) for 1) (tldr: the exception message is correct IMO).

@QuLogic
Copy link
Member

QuLogic commented May 15, 2020

Case 1 was incorrect and is fixed on master now, by #17245. So I believe all 3 cases are now fixed.

Sorry this got stuck @davidleejy, but I'm going to close this now as unneeded.

@QuLogic QuLogic closed this May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants