我目前有以下代码:
我还导入了numpy和matplotlib库。
def colorTheCluster(data,centroidCoordinates):
index = AssignPointsToCluster(data,centroidCoordinates) #index contains an 1d array of size 1000 of values [0 1 2 1 3 0....]
indexList = list(index) # i wanted to make it into a list so i can use a for loop
colorMap = {0:'r', 1 : 'b', 2:"y", 3: 'p', 4 :'g'}
for num in indexList: #[0,1,1,2,3,0,0...]
for keys in colorMap.keys(): #{1, 2, 0 , 4}
if keys == num:
plt.scatter(data[:,0], data[:,1], marker = '+', c = colorMap[0])
elif keys == num:
plt.scatter(data[:,0], data[:,1], marker = '+', c = colorMap[1])
elif keys == num:
plt.scatter(data[:,0], data[:,1], marker = '+', c = colorMap[2])
elif keys == num:
plt.scatter(data[:,0], data[:,1], marker = '+', c = colorMap[3])
elif keys == num:
plt.scatter(data[:,0], data[:,1], marker = '+', c = colorMap[4])
return plt.show()
print(colorTheCluster(data(), centroidCoordinates()))
data()
是一个包含1000 x 2数组的函数
centroidCoordinates()
是m x2的数组(在这种情况下,我使用m = 4)
我希望为群集中分配索引的每个点相应地更改颜色,但似乎不起作用。
答案 0 :(得分:0)
12-12 13:20:04.393 10226-10226/net.beaconsdkdemo I/CycledLeScanner: Adjusted nextScanStartTime to be Wed Dec 12 13:15:21 GMT+11:00 2018
函数接受参数plt.scatter
–这可以是与数据长度相同的列表/数组。
c
要定义特定的颜色,您要制作一个颜色图:
# make some fake data
data = np.random.random([1000,2])
index = np.random.choice([0,1,2,3], size=len(data))
# scatter plot the data, using `index` to indicate color
plt.scatter(data[:,0], data[:,1], c=index)
# or alternatively:
plt.scatter(*data.T, c=index)
请注意,我用import matplotlib.colors as mcols
# define a colormap using a list of custom colors
cmap = mcols.ListedColormap(['r','b','y','purple','g'])
# scatter plot the data using your new colormap
plt.scatter(*data.T, c=index, cmap=cmap)
代替了'p'
,这是matplotlib无法理解的颜色代码。