You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Scatter plot color array length should raise Error #3079
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.
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.
Activity
GregoryAshton commentedon May 28, 2014
I believe this behaviour to be unintentional. The
cargument can also take a 2-D array of RGB or RGBA values (between 0-1). Ifmatplotlibthinks this is the case it is passed tomatplotlib.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_arrayin which cases it passes it ontomatplotlib.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:So is the first 3 entries in
colorsare 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 whilecolors=[1.1, 0.0, 0.0, 10.0, 100.0]will produce an error as thervalue is greater than1.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 commentedon May 28, 2014
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 commentedon Jun 2, 2014
fix in #3092 has been merged.