Skip to content

Scatter plot color array length should raise Error #3079

Closed

Description

@hughesadam87

Not sure if this behavior is intentional, but if one makes a scatter plot and provides an array for c which is longer than the number of x, y values, instead of raising an error from the arrays being unevenly shaped, it filled in the colors in incorrectly. Here is an example with the correct colors (because 'colors' is the same length as x and y)

x = np.linspace(0,10)
y = np.sin(x)
colors = x
plt.scatter(x,y, c=colors, cmap=mpl.cm.RdBu, s=100)

Here is one where colors is twice the size of x and y:

x = np.linspace(0,10)
y = np.sin(x)
colors = np.linspace(0,10, 100)
plt.scatter(x,y, c=colors, cmap=mpl.cm.RdBu, s=100)

Seems like the intended behavior would be to have an error thrown, or am I mistaken.

Activity

  1. GregoryAshton commented on May 28, 2014

    @GregoryAshton
    Contributor

    I believe this behaviour to be unintentional. The c argument can also take a 2-D array of RGB or RGBA values (between 0-1). If matplotlib thinks this is the case it is passed to matplotlib.colors.ColorConverter.to_rgba_array(c. alpha).

    The problem is that this function tests if it has been given a single value for the rgba_array in which cases it passes it onto matplotlib.colors.ColorConverter.to_rgba(c. alpha).

    Now I have no idea how else this function is used but in this case the error is this. The function only checks that all of entries are bounded between [0, 1] if the length of the array is 4. So in this case length of array is anything then it takes:

     r, g, b = arg[:3]
    

    So is the first 3 entries in colors are between [0, 1] then a single RGB color value is chosen defined by these producing the unexpected behaviour.

    To check this, in the example given by @hugadams if colors=[1.0, 0.0, 0.0, 10.0, 100.0] it will pass all checks and produce a red coloured plot while colors=[1.1, 0.0, 0.0, 10.0, 100.0] will produce an error as the r value is greater than 1.

    As far as I can tell this just needs a small change to check the RGBA single value is of length 4 and this print a suitable error message. As I am new could anyone confirm this and I will be happy to try and implement a PR.

  2. added this to the v1.4.x milestone on May 28, 2014
  3. tacaswell commented on May 28, 2014

    @tacaswell
    Member

    Tagged as 1.4.x, but if this gets done before we tag 1.4.0 I see no reason it can't go in.

    @ga7g08 Please go ahead at try to fix this. I can't quite follow what you are proposing to change, the clearest way to see it is as a pull request.

  4. tacaswell commented on Jun 2, 2014

    @tacaswell
    Member

    fix in #3092 has been merged.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions