首先,根据y中的值设置颜色,你可以这样做:
color = ['red' if i==0 else 'green' for i in y]
plt.scatter(X[:,0], X[:,1], c=color)
现在谈谈scatter() 和cmap。
ColorMaps 用于根据浮点值提供颜色。见this documentation for reference on colormaps。
对于 0 到 1 之间的值,从这些颜色图中选择一种颜色。
例如:
plt.cm.Spectral(0.0)
# (0.6196078431372549, 0.00392156862745098, 0.25882352941176473, 1.0) #<== magenta
plt.cm.Spectral(1.0)
# (0.3686274509803922, 0.30980392156862746, 0.6352941176470588, 1.0) #<== blue
plt.cm.Spectral(1)
# (0.6280661284121491, 0.013302575932333718, 0.26082276047673975, 1.0)
请注意,上面代码中 1.0 和 1 的结果是不同的,因为 __call__() here 的文档中提到的 int 和 float 的处理方式不同:
对于浮点数,X 应该在区间 [0.0, 1.0] 中以返回
RGBA 值 X*100 沿颜色图线的百分比。
对于整数,X 应该在区间[0, Colormap.N) 到
从索引为 X 的颜色图中返回 RGBA 值索引。
请查看此答案以获得有关颜色图的更多更好解释:-
在您的 y 中,您有 0 和 1,因此使用上面代码中显示的 RGBA 值(它们代表 Spectral 颜色图的两端)。
下面是plt.scatter() 中的c 和cmap 参数如何相互交互。
_______________________________________________________________________
|No | type of x, y | c type | values in c | result |
|___|______________|__________|_____________|___________________________|
|1 | single | scalar | numbers | cmap(0.0), no matter |
| | point | | | what the value in c |
|___|______________|__________|_____________|___________________________|
|2 | array of | array | numbers | normalize the values in c,|
| | points | | | cmap(normalized val in c) |
|___|______________|__________|_____________|___________________________|
|3 | scalar or | scalar or| RGBA Values,| no use of cmap, |
| | array | array |Color Strings| use colors from c |
|___|______________|__________|_____________|___________________________|
现在,一旦最终确定了实际颜色,然后循环遍历x, y 中每个点的颜色。如果 x, y 的大小等于或小于 c 中颜色的大小,那么你得到了完美的映射,否则会再次使用旧的颜色。
这里有一个例子来说明这一点:
# Case 1 from above table
# All three points get the same color = plt.cm.Spectral(0)
plt.scatter(x=0.0, y=0.2, c=0, cmap=plt.cm.Spectral)
plt.scatter(x=0.0, y=0.3, c=1, cmap=plt.cm.Spectral)
plt.scatter(x=0.0, y=0.4, c=1.0, cmap=plt.cm.Spectral)
# Case 2 from above table
# The values in c are normalized
# highest value in c gets plt.cm.Spectral(1.0)
# lowest value in c gets plt.cm.Spectral(0.0)
# Others in between as per normalizing
# Size of arrays in x, y, and c must match here, else error is thrown
plt.scatter([0.1, 0.1, 0.1, 0.1, 0.1], [0.2, 0.3, 0.4, 0.5, 0.6],
c=[1, 2, 3, 4, 5], cmap=plt.cm.Spectral)
# Case 3 from above table => No use of cmap here,
# blue is assigned to the point
plt.scatter(x=0.2, y=0.3, c='b')
# You can also provide rgba tuple
plt.scatter(x=0.2, y=0.4, c=plt.cm.Spectral(0.0))
# Since a single point is present, the first color (green) is given
plt.scatter(x=0.2, y=0.5, c=['g', 'r'])
# Same color 'cyan' is assigned to all values
plt.scatter([0.3, 0.3, 0.3, 0.3, 0.3], [0.2, 0.3, 0.4, 0.5, 0.6],
c='c')
# Colors are cycled through points
# 4th point will get again first color
plt.scatter([0.4, 0.4, 0.4, 0.4, 0.4], [0.2, 0.3, 0.4, 0.5, 0.6],
c=['m', 'y', 'k'])
# Same way for rgba values
# Third point will get first color again
plt.scatter([0.5, 0.5, 0.5, 0.5, 0.5], [0.2, 0.3, 0.4, 0.5, 0.6],
c=[plt.cm.Spectral(0.0), plt.cm.Spectral(1.0)])
输出:
通过代码中的 cmets 和点的位置以及颜色来彻底理解。
您也可以将案例3代码中的参数c替换为color,结果还是一样。