-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Plotting three points using scatter results in wrong color - 1.4.3 #5377
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
Comments
Confirmed on master. |
Thanks for taking a look. I tried the workaround but I don't see any change. With nested color arrays: Here's my attempt:
|
You have it backwards. In your examples, the scatter plots with three points are correct, the other plots are wrong. The documentation for 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 Note that the results depend on the current default colormap, as expected: import matplotlib.pyplot as plt
import numpy as np
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
plt.rcParams['image.cmap'] = 'jet'
points = np.random.rand(2,3)
ax1.scatter(points[0], points[1], c=[0, 1, 0.3], s=200)
plt.rcParams['image.cmap'] = 'gray'
points = np.random.rand(2,3)
ax2.scatter(points[0], points[1], c=[0, 1, 0.3], s=200)
plt.rcParams['image.cmap'] = 'cool'
points = np.random.rand(2,3)
ax3.scatter(points[0], points[1], c=[0, 1, 0.3], s=200)
plt.rcParams['image.cmap'] = 'BrBG'
points = np.random.rand(2,3)
ax4.scatter(points[0], points[1], c=[0, 1, 0.3], s=200)
plt.show() |
TIL that the Shouldn't nesting work though? From my last reply, the nesting is being completely ignored, even though the documentation says otherwise, e.g. From the docs:
My workaround is to duplicate the rows, e.g. |
I agree that Another possible workaround is to use import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import rgb2hex
plt.rcParams['image.cmap'] = 'gray'
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2)
points = np.random.rand(2,3)
ax1.scatter(points[0], points[1], s=200, c=[[0, 1, 0]]) # Wrong!
ax2.scatter(points[0], points[1], s=200, c=(points.shape[1]*[[0, 1, 0]])) # Works Correctly
ax3.scatter(points[0], points[1], s=200, c=np.repeat([[0, 1, 0]], points.shape[1], axis=0)) # Works Correctly
ax4.scatter(points[0], points[1], s=200, c=rgb2hex([0, 1, 0])) # Works Correctly
plt.show() |
I confirm that both workarounds get the intended results. Thank you both for your help. |
Same issue with RGBA tuple. This time the bug shows up when plotting 4 points. import matplotlib.pyplot as plt
x = [[1],
[2, 2],
[3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7],
[8, 8],
[9]]
y = [list(range(len(_x))) for _x in x]
facecolor = (1.0, 1.0, 1.0, .5)
fig, ax = plt.subplots(1)
for i in range(len(x)):
ax.scatter(x=x[i],
y=y[i],
facecolor=facecolor,
s=15**2)
plt.show() |
I got bitten by this behavior several times now. Seeing #6087 being closed woke me up. I understand that underneath we convert to ndarray, but the public interface seems pretty unambiguous to me. |
The last example now works correctly on 2.x (more-or-less, the default edge width and color changed to 0 and 'face') import matplotlib.pyplot as plt
x = [[1],
[2, 2],
[3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7],
[8, 8],
[9]]
y = [list(range(len(_x))) for _x in x]
facecolor = (1, 1, 1, .5)
fig, ax = plt.subplots(1)
for i in range(len(x)):
ax.scatter(x=x[i],
y=y[i],
facecolor=facecolor,
s=15**2,
linewidths=1,
edgecolors='k')
plt.show() The fundemental problem here is that sometimes |
On 2016/05/16 10:34 AM, wavexx wrote:
No, because that would eliminate the use of a 2-D ndarray, (n,3) or With the most recent change, if you want a single color for everything, The design error here, from early days (and probably from copying |
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). |
Closing as given the extra degrees of freedom in |
I think the subtle difference between |
Fair enough. Re-milestoned for 2.2, I am trying to use the 2.1.1 milestone to manage the 2.1.1 release! |
that is totally fine with me :-) |
I think this is sufficiently documented now, from e.g., #17499. |
I'm running this on Windows 10 / Anaconda Python 3.4.3 / jupyter iPython 4
Here's some repro code:
Here's what I get:
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.
The text was updated successfully, but these errors were encountered: