Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve error when passing 0d array to scatter(). #15883

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
Dec 12, 2019

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Dec 9, 2019

With scatter([1, 2], [3, 4], c=np.array(.5))...

previously:

<elided>
  File ".../matplotlib/colors.py", line 338, in to_rgba_array
    if len(c) == 0:
TypeError: len() of unsized object

now

<elided>
  File ".../matplotlib/axes/_axes.py", line 4249, in _parse_scatter_color_args
    raise invalid_shape_exception(c.size, xsize)
ValueError: 'c' argument has 1 elements, which is inconsistent with 'x' and 'y' with size 2.

Essentially the problem is that np.array(.5) passes an
isinstance(..., Iterable) check (which only checks the type) but
np.iterable() more correctly returns False because it actually tries to
iter on it.

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

With `scatter([1, 2], [3, 4], c=np.array(.5))`...

previously:

    <elided>
      File ".../matplotlib/colors.py", line 338, in to_rgba_array
        if len(c) == 0:
    TypeError: len() of unsized object

now

    <elided>
      File ".../matplotlib/axes/_axes.py", line 4249, in _parse_scatter_color_args
        raise invalid_shape_exception(c.size, xsize)
    ValueError: 'c' argument has 1 elements, which is inconsistent with 'x' and 'y' with size 2.

Essentially the problem is that np.array(.5) passes an
`isinstance(..., Iterable)` check (which only checks the type) but
np.iterable() more correctly returns False because it actually tries to
iter on it.
@jklymak jklymak merged commit 3c58e2a into matplotlib:master Dec 12, 2019
@QuLogic QuLogic added this to the v3.3.0 milestone Dec 24, 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