简体   繁体   English

散布 plot 有 20 多个标签和不同的 colors

[英]Scatter plot with more than 20 labels and different colors

I want to plot the result of a clustering with 40 clusters.我想要 plot 具有 40 个集群的集群结果。 I have this code:我有这段代码:

plt.figure(figsize=(14,10))
for i in labels:
    plt.scatter(df.iloc[cluster == i , 0] ,
                df.iloc[cluster == i , 1], label = i, alpha= 0.8)
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5),ncol = 2)
plt.show()

and the result is:结果是: 在此处输入图像描述 As you can see 20 colors are repeating.如您所见,有 20 个 colors 在重复。 How can I assign 40 different colors to these clusters?如何为这些集群分配 40 个不同的 colors?

I've seen this link: Matplotlib color according to class labels我看过这个链接: Matplotlib color according to class labels

But it didn't solve my problem.但这并没有解决我的问题。

You should be able to use the c parameter of the scatter function to specify the colour, like this:您应该能够使用散点图function 的c参数来指定颜色,如下所示:

Code:代码:

plt.figure(figsize=(14,10))
for i in labels:
    plt.scatter(df.iloc[cluster == i , 0] , df.iloc[cluster == i , 1], label = i, alpha= 0.8, c=i, cmap='viridis')
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5),ncol = 2)
plt.show()

Alternatively you can specify your own colour list like this:或者,您可以像这样指定自己的颜色列表:

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 'brown', 'pink', 'gray', 'olive', 'cyan', 'navy', 'teal', 'maroon', 'silver', 'tan', 'gold', 'purple', 'moccasin', 'bisque', 'wheat', 'peachpuff', 'navajowhite', 'salmon', 'crimson', 'palevioletred', 'darksalmon', 'lightcoral', 'hotpink', 'palegoldenrod', 'plum', 'darkkhaki', 'orchid', 'thistle', 'lightgray', 'lightgreen', 'lightblue', 'lightskyblue', 'lightyellow', 'lavender', 'linen']

plt.figure(figsize=(14,10))
for i in labels:
    plt.scatter(df.iloc[cluster == i , 0] , df.iloc[cluster == i , 1], label = i, alpha= 0.8, color=colors[i])
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5),ncol = 2)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM