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

Skip to content

Passing an incorrectly sized colour list to scatter should raise a relevant error #9209

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
MatthewJA opened this issue Sep 21, 2017 · 11 comments

Comments

@MatthewJA
Copy link

If the length of the c argument to matplotlib.pyplot.scatter doesn't match the size of the x and y arguments, a confusing error message is raised: Invalid RGBA argument. Contrast this with the error that results if x and y are not the same size: x and y must be the same size. The error message for an incorrect size c array should be made more clear.

Code for reproduction

import numpy, matplotlib.pyplot as plt
xs, ys = numpy.random.random((2, 10))
c = numpy.random.random(9)
plt.scatter(xs, ys, c=c)

Actual outcome

/usr/local/lib/python3.6/site-packages/matplotlib/colors.py in _to_rgba_no_colorcycle(c, alpha)
    192         # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
    193         # Test dimensionality to reject single floats.
--> 194         raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
    195     # Return a tuple to prevent the cached value from being modified.
    196     c = tuple(c.astype(float))

ValueError: Invalid RGBA argument: 0.58430716187559439

Expected outcome

ValueError: c and x must be the same size
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6.1
@jklymak
Copy link
Member

jklymak commented Sep 21, 2017

This is fixed in master. Should be fixed for 2.1 release. The error raised is

ValueError: c of shape (9,) not acceptable as a color sequence for x with size 10, y with size 10

@jklymak
Copy link
Member

jklymak commented Sep 21, 2017

Closing - feel free to re-open if you think its not fixed in the upcoming 2.1. Thanks for the report!

@jklymak jklymak closed this as completed Sep 21, 2017
@jklymak
Copy link
Member

jklymak commented Sep 21, 2017

Fixed in #7363

@PorkShoulderHolder
Copy link

I am getting this error ValueError: c of shape (3854,) not acceptable as a color sequence for x with size 3854, y with size 3854 in version 2.2.4 of matplotlib. The error definitely isn't helpful in this case.

@sskgp
Copy link

sskgp commented Jun 18, 2019

@PorkShoulderHolder please change c to have a shape of (n,1) instead of (n,) - this works for me.

@weihuaguo
Copy link

@sabyagunnersya I try both (n,1) and (n,). Both of them are not working! Is there any other solution? Thanks!
ValueError: c of shape (303262, 1) not acceptable as a color sequence for x with size 303262, y with size 303262
ValueError: c of shape (303262,) not acceptable as a color sequence for x with size 303262, y with size 303262

@TTitcombe
Copy link

@PorkShoulderHolder @weihuaguo Is this the issue you were seeing? #15727

@runnholm
Copy link

@PorkShoulderHolder please change c to have a shape of (n,1) instead of (n,) - this works for me.

This works for me as well, but this does not seem like expected behaviour? There is no obvious reason (n,) should not work.

@sorenwacker
Copy link

LoL, I just got this:

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

@tacaswell
Copy link
Member

@soerendip What version of Matplotlib? I think we have improved that error message recently.

Do you have a minimal example that will produce that error?

@ngmaihuong
Copy link

ngmaihuong commented Aug 10, 2020

@soerendip What version of Matplotlib? I think we have improved that error message recently.

Do you have a minimal example that will produce that error?

I got the same error:
ValueError: 'c' argument has 59 elements, which is not acceptable for use with 'x' with size 59, 'y' with size 59.

My version of Matplotlib is 3.1.3
Full error:

Traceback (most recent call last):

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 4284, in _parse_scatter_color_args
    colors = mcolors.to_rgba_array(c)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/colors.py", line 294, in to_rgba_array
    result[i] = to_rgba(cc, alpha)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/colors.py", line 177, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/colors.py", line 240, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))

ValueError: Invalid RGBA argument: 1


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/Users/Python Files/multiclass.py", line 54, in <module>
    c=y_fruits_2d, cmap=cmap_fruits, edgecolor = 'black', alpha=.7)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2848, in scatter
    None else {}), **kwargs)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1599, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 4453, in scatter
    get_next_color_func=self._get_patches_for_fill.get_next_color)

  File "/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 4297, in _parse_scatter_color_args
    .format(nc=n_elem, xs=xsize, ys=ysize)

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

I've fixed it though -- changed Y to Y.values.reshape(Y.shape[0])

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

No branches or pull requests

10 participants