Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
davidleejy wants to merge4 commits intomatplotlib:masterfromdavidleejy:err-msg-fix

Conversation

davidleejy
Copy link

@davidleejydavidleejy commentedAug 24, 2018
edited
Loading

PR Summary

Fixes issue#11919 .

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

Prior to this PR, exceptions are not chained - see commite3a8004. Exceptions raised bymcolors.to_rgba_array() were suppressed by exceptions of its callerscatter().

This PR applies exception chaining solely onscatter 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 eValueError: '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 isFlake 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

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

@timhoffm
Copy link
Member

The test case forc=[0.5]*5 intest_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.

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

davidleejy commentedAug 27, 2018
edited
Loading

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 frommcolors.to_rgba_array(), then tests forto_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

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

@joseortiz3
Copy link

joseortiz3 commentedJun 24, 2019
edited
Loading

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

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).

joseortiz3 reacted with confused emoji

@QuLogic
Copy link
Member

Case 1was 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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@davidleejy@jklymak@timhoffm@anntzer@joseortiz3@QuLogic

[8]ページ先頭

©2009-2025 Movatter.jp