8000 Suppress chaining of cache lookup failure in color conversion. by anntzer · Pull Request #13468 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Suppress chaining of cache lookup failure in color conversion. #13468

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
Feb 20, 2019

Conversation

anntzer
Copy link
Contributor
@anntzer anntzer commented Feb 19, 2019

Currently, something like
scatter(range(4), range(4), c=np.arange(4).reshape((2, 2)))
results in the following traceback:

Traceback (most recent call last):
  File "matplotlib/lib/matplotlib/colors.py", line 173, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "matplotlib/lib/matplotlib/axes/_axes.py", line 4289, in _parse_scatter_color_args
    colors = mcolors.to_rgba_array(c)
  File "matplotlib/lib/matplotlib/colors.py", line 284, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "matplotlib/lib/matplotlib/colors.py", line 175, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "matplotlib/lib/matplotlib/colors.py", line 240, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "matplotlib/lib/matplotlib/pyplot.py", line 2826, in scatter
    None else {}), **kwargs)
  File "matplotlib/lib/matplotlib/__init__.py", line 1512, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "matplotlib/lib/matplotlib/axes/_axes.py", line 4458, in scatter
    get_next_color_func=self._get_patches_for_fill.get_next_color)
  File "matplotlib/lib/matplotlib/axes/_axes.py", line 4302, in _parse_scatter_color_args
    .format(nc=n_elem, xs=xsize, ys=ysize)
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 4, 'y' with size 4.

At least the topmost failure, regarding cache lookup, is irrelevant to
the end user, so suppress it, which this commit does.

The middle traceback is mildly interesing, in case the user intended to
pass in RGBA but mis-shaped it; we may want to fold the message into the
scatter() error as well -- but that's not the object of this commit.

PR Summary

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

Currently, something like
`scatter(range(4), range(4), c=np.arange(4).reshape((2, 2)))`
results in the following traceback:

```
Traceback (most recent call last):
  File "matplotlib/lib/matplotlib/colors.py", line 173, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "matplotlib/lib/matplotlib/axes/_axes.py", line 4289, in _parse_scatter_color_args
    colors = mcolors.to_rgba_array(c)
  File "matplotlib/lib/matplotlib/colors.py", line 284, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "matplotlib/lib/matplotlib/colors.py", line 175, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "matplotlib/lib/matplotlib/colors.py", line 240, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "matplotlib/lib/matplotlib/pyplot.py", line 2826, in scatter
    None else {}), **kwargs)
  File "matplotlib/lib/matplotlib/__init__.py", line 1512, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "matplotlib/lib/matplotlib/axes/_axes.py", line 4458, in scatter
    get_next_color_func=self._get_patches_for_fill.get_next_color)
  File "matplotlib/lib/matplotlib/axes/_axes.py", line 4302, in _parse_scatter_color_args
    .format(nc=n_elem, xs=xsize, ys=ysize)
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 4, 'y' with size 4.
```

At least the topmost failure, regarding cache lookup, is irrelevant to
the end user, so suppress it, which this commit does.

The middle traceback is mildly interesing, in case the user intended to
pass in RGBA but mis-shaped it; we may want to fold the message into the
scatter() error as well -- but that's not the object of this commit.
@timhoffm timhoffm merged commit 10aee8d into matplotlib:master Feb 20, 2019
@anntzer anntzer deleted the colorexceptionchaining branch February 20, 2019 19:18
@QuLogic QuLogic added this to the v3.1.0 milestone Feb 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0