Skip to content

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

Closed

Description

@MatthewJA

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

Activity

  1. jklymak commented on Sep 21, 2017

    @jklymak
    Member

    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
    
  2. jklymak commented on Sep 21, 2017

    @jklymak
    Member

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

  3. jklymak commented on Sep 21, 2017

    @jklymak
    Member

    Fixed in #7363

  4. PorkShoulderHolder commented on Feb 22, 2019

    @PorkShoulderHolder

    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.

  5. sskgp commented on Jun 18, 2019

    @sskgp

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

  6. weihuaguo commented on Oct 4, 2019

    @weihuaguo

    @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

  7. TTitcombe commented on Nov 19, 2019

    @TTitcombe

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

  8. runnholm commented on Nov 26, 2019

    @runnholm

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

  9. sorenwacker commented on Jun 2, 2020

    @sorenwacker

    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.
    
  10. tacaswell commented on Jun 2, 2020

    @tacaswell
    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?

  11. ngmaihuong commented on Aug 10, 2020

    @ngmaihuong

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions