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
Plotting three points using scatter results in wrong color - 1.4.3 #5377
As you can see, plotting 2, 4, or any other number of points using ax.scatter is fine, but graphing 3 points while using RGB color array results in two points as black, and one point as white, instead of using the specified color.
Here's an example on a real dataset. As you can see, it also messes with the legend color. It's hard to diagnose the issue because it's completely non-obvious that the number of points plotted is what triggers the bug.
I was pulling my hair out for awhile until I realized that the problem only manifested itself on series that had 3 members.
Confirmed on master.
As a workaround, if you use c=[[0, 1, 0]] it will work.
Without the workaround, I get 2 black and 1 magenta rather than white; but in any case it is a bug.
You have it backwards. In your examples, the scatter plots with three points are correct, the other plots are wrong. The documentation for scatter says:
c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.
(emphasis mine)
The problem, in my opinion, is that scatter should raise a ValueError if c is a sequence and does not equal the number of points, instead of trying to interpret sequences of length=3 as RGB values, and sequences of length=4 as RGBA values, as it currently does.
Note that the results depend on the current default colormap, as expected:
TIL that the c argument expects a vector of colors.
Shouldn't nesting work though? From my last reply, the nesting is being completely ignored, even though the documentation says otherwise, e.g. ax.scatter(points[0], points[1], c=[[0, 1, 0]], s=200) is interpreted as if it were c=[0, 1, 0]
From the docs:
c can be a 2-D array in which the rows are RGB or RGBA, however, including the case of a single row to specify the same color for all points.
My workaround is to duplicate the rows, e.g. c = [[0, 1, 0],] * n_points
I agree that c=[[0, 1, 0]] should work as a single RGB color, but matplotlib 1.5.0 incorrectly treats it the same as c=[0, 1, 0]. This is definitely a bug.
Another possible workaround is to use matplotlib.colors.rgb2hex. Example usage:
I got bitten by this behavior several times now. Seeing #6087 being closed woke me up.
Can't we restrict color specs to be always tuples and remove the ambiguity?
I understand that underneath we convert to ndarray, but the public interface seems pretty unambiguous to me.
The fundemental problem here is that sometimes c is meant as a 'color spec', that is an RGB on RGBA tuple, or a string (either a color name or a float string which maps to shades of gray) OR an array of the same size of x and y which should be color mapped. In the case of length 3 and 4 there is no way to break user intention so we go with color mapping. In the case of the kwarg color it is always a color.
Closing as given the extra degrees of freedom in c we are breaking the ambiguity in a reasonable way and if the user wants to set the (rather than go through the color mapping framework via 'c') they can use color or facecolor/edgecolor.
I think the subtle difference between c and color could at least be better documented (e.g. in the docstring entry for c?). Feel free to reclose if you disagree.
Activity
efiring commentedon Nov 2, 2015
Confirmed on master.
As a workaround, if you use
c=[[0, 1, 0]]it will work.Without the workaround, I get 2 black and 1 magenta rather than white; but in any case it is a bug.
liujimj commentedon Nov 2, 2015
Thanks for taking a look.
I tried the workaround but I don't see any change. With nested color arrays:
Here's my attempt:
u55 commentedon Nov 2, 2015
You have it backwards. In your examples, the scatter plots with three points are correct, the other plots are wrong. The documentation for
scattersays:c can be a single color format string, or a sequence of color specifications of length N, or a sequence of N numbers to be mapped to colors using the cmap and norm specified via kwargs (see below). Note that c should not be a single numeric RGB or RGBA sequence because that is indistinguishable from an array of values to be colormapped. c can be a 2-D array in which the rows are RGB or RGBA, however.
(emphasis mine)
The problem, in my opinion, is that
scattershould raise a ValueError ifcis a sequence and does not equal the number of points, instead of trying to interpret sequences of length=3 as RGB values, and sequences of length=4 as RGBA values, as it currently does.Note that the results depend on the current default colormap, as expected:
liujimj commentedon Nov 3, 2015
TIL that the
cargument expects a vector of colors.Shouldn't nesting work though? From my last reply, the nesting is being completely ignored, even though the documentation says otherwise, e.g.
ax.scatter(points[0], points[1], c=[[0, 1, 0]], s=200)is interpreted as if it werec=[0, 1, 0]From the docs:
My workaround is to duplicate the rows, e.g.
c = [[0, 1, 0],] * n_pointsu55 commentedon Nov 4, 2015
I agree that
c=[[0, 1, 0]]should work as a single RGB color, but matplotlib 1.5.0 incorrectly treats it the same asc=[0, 1, 0]. This is definitely a bug.Another possible workaround is to use
matplotlib.colors.rgb2hex. Example usage:liujimj commentedon Nov 7, 2015
I confirm that both workarounds get the intended results. Thank you both for your help.
has2k1 commentedon Nov 14, 2015
Same issue with RGBA tuple. This time the bug shows up when plotting 4 points.
wavexx commentedon May 16, 2016
I got bitten by this behavior several times now. Seeing #6087 being closed woke me up.
Can't we restrict color specs to be always tuples and remove the ambiguity?
I understand that underneath we convert to ndarray, but the public interface seems pretty unambiguous to me.
tacaswell commentedon May 16, 2016
The last example now works correctly on 2.x (more-or-less, the default edge width and color changed to 0 and 'face')
The fundemental problem here is that sometimes
cis meant as a 'color spec', that is an RGB on RGBA tuple, or a string (either a color name or a float string which maps to shades of gray) OR an array of the same size of x and y which should be color mapped. In the case of length 3 and 4 there is no way to break user intention so we go with color mapping. In the case of the kwargcolorit is always a color.efiring commentedon May 17, 2016
On 2016/05/16 10:34 AM, wavexx wrote:
No, because that would eliminate the use of a 2-D ndarray, (n,3) or
(n,4), for a sequence of colorspecs. Or a row indexed from such an array.
With the most recent change, if you want a single color for everything,
use the "color" kwarg.
The design error here, from early days (and probably from copying
Matlab), is trying to use a single kwarg for too many different things.
wavexx commentedon May 24, 2016
The answer from @efiring settles my confusion about allowed types. I didn't realize 2d ndarrays were allowed as well.
My only fear is the subtle difference in c/color semantics used in existing functions (where c/color are aliases).
3 remaining items
tacaswell commentedon Oct 21, 2017
Closing as given the extra degrees of freedom in
cwe are breaking the ambiguity in a reasonable way and if the user wants to set the (rather than go through the color mapping framework via 'c') they can usecolororfacecolor/edgecolor.anntzer commentedon Oct 21, 2017
I think the subtle difference between
candcolorcould at least be better documented (e.g. in the docstring entry forc?). Feel free to reclose if you disagree.tacaswell commentedon Oct 21, 2017
Fair enough.
Re-milestoned for 2.2, I am trying to use the 2.1.1 milestone to manage the 2.1.1 release!
anntzer commentedon Oct 21, 2017
that is totally fine with me :-)
QuLogic commentedon Jul 21, 2020
I think this is sufficiently documented now, from e.g., #17499.