Skip to content
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

Scatter plot color array length should raise Error #3079

Closed
hughesadam87 opened this issue May 20, 2014 · 3 comments
Closed

Scatter plot color array length should raise Error #3079

hughesadam87 opened this issue May 20, 2014 · 3 comments
Milestone

Comments

@hughesadam87
Copy link
Contributor

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.

@GregoryAshton
Copy link
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.

@tacaswell tacaswell added this to the v1.4.x milestone May 28, 2014
@tacaswell
Copy link
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.

@tacaswell
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants