为 python 中的聚类图分配独特的颜色

assigning a unique color to the plot of clusters in python

我有一个分为 27 个簇的数据框。为了仅在一个图中绘制所有这些集群,我使用以下 for 循环:

list_of_clusters= list(set(klabels))
    
fig=plt.figure(figsize=(13,9),frameon=True,facecolor='lightgrey', edgecolor='black')
    ax = fig.add_subplot(1,1,1)
    plt.axis()
    plt.xlim([-2.5, 0.2])
    plt.ylim([-0.7, 3.3])
    plt.xlabel("log PhiZ")
    plt.ylabel("log RQI")
    
    for i in list_of_clusters:
        
        plt.scatter(
        logphizlogrqi[klabels == i, 0], logphizlogrqi[klabels == i, 1],
        s=10, cmap='hsv',
        marker='8',
        label=i+1
        )
    
    
    
    ax.yaxis.tick_right()
    ax.yaxis.set_ticks_position('both')
    plt.legend(scatterpoints=1, loc='center left',bbox_to_anchor=(-0.4, 0.5))
    plt.grid()
    plt.show()

但是生成的图形多次使用每种颜色,如下图所示:

理想情况下,我要查找的图形应该如下所示(虽然颜色彼此接近,但它们只使用一次):

如果你能帮我解决我的问题,我将不胜感激

请在seaborn示例中添加并修复以下代码以回应评论。

添加。

import seaborn as sns

NUM_COLORS = 27

sns.reset_orig() 
colors = sns.color_palette('husl', n_colors=NUM_COLORS)

正在编辑

for i in list_of_clusters:
    plt.scatter(
    logphizlogrqi[klabels == i, 0], logphizlogrqi[klabels == i, 1],
    s=10, cmap='hsv',
    marker='8',
    label=i+1,
    color=colors[i] # update
    )

所以我想出了这个方法并且效果很好:

from matplotlib import colors as mcolors
import random

list_of_clusters= list(set(klabels))

colors = list(mcolors.CSS4_COLORS.keys())
random.shuffle(colors)

fig=plt.figure(figsize=(13,9),frameon=True,facecolor='lightgrey', edgecolor='black')
ax = fig.add_subplot(1,1,1)
plt.axis()
plt.xlim([-2.5, 0.2])
plt.ylim([-0.7, 3.3])
plt.xlabel("log PhiZ")
plt.ylabel("log RQI")

for i in list_of_clusters:
    plt.scatter(
    logphizlogrqi[klabels == i, 0], logphizlogrqi[klabels == i, 1],
    s=10,
    marker='8',
    label=i+1,
    color=colors[i]
    )

ax.yaxis.tick_right()
ax.yaxis.set_label_position('right')
ax.yaxis.set_ticks_position('right')
plt.legend(scatterpoints=1, loc='center left',bbox_to_anchor=(-0.4, 0.5))
plt.show()

结果如下所示: