简体   繁体   English

用完全不同的颜色绘制散点图

[英]Plotting scatterplot with completely different colors

I am trying to create a scatterplot, however, I'm getting repeated colors for different classes(I have 10 classes).我正在尝试创建一个散点图,但是,我得到了不同类别的重复颜色(我有 10 个类别)。

from sklearn.decomposition import PCA
from sklearn.cluster import MiniBatchKMeans

pca = PCA(n_components=2, random_state=7)
reduced_features = pca.fit_transform(X_idf.toarray())
cls = MiniBatchKMeans(n_clusters=10, random_state=7)
cls.fit(X_idf)

pred = cls.predict(X_idf)
plt.scatter(reduced_features[:,0], reduced_features[:,1], c=pred, )
plt.scatter(reduced_cluster_centers[:, 0], reduced_cluster_centers[:,1], marker='x', s=200, 
c='b')
plt.title('K-means data distribution')

I have tried adding some color maps (eg: cmap='bwr') at the first pl.scatter() call, but it doesn't solve my problem.我曾尝试在第一次 pl.scatter() 调用中添加一些颜色贴图(例如:cmap='bwr'),但它并没有解决我的问题。

My Y data (c=pred) is a list from 0 to 10. Value used for the plot below [...0 9 5 1 1 1 1 8 1 4 6 4 7 2 0 4 9 9 9 9 4 4 5 5 5 4 4 4 4 3 4 7 1 1 1 1 1 7 4 2 2 2 2 4 8 8 8 0 8 4 4 4 4 7 4 3 3 3 3 4 3 4 4 4 2 5 4 2 7 ...]我的 Y 数据 (c=pred) 是一个从 0 到 10 的列表。用于下图的值 [...0 9 5 1 1 1 1 8 1 4 6 4 7 2 0 4 9 9 9 9 4 4 5 5 5 4 4 4 4 3 4 7 1 1 1 1 7 4 2 2 2 2 4 8 8 8 0 8 4 4 4 7 4 3 3 3 3 4 3 4 4 4 2 5 4 2 7 ...]

This is my current plot:这是我目前的情节:

当前情节

Does anyone have a clue on how to keep the c parameters as the predicted class, but have different colors that would allow me to visualize it better?有没有人知道如何将 c 参数保留为预测类,但有不同的颜色可以让我更好地对其进行可视化?

for anyone looking for a colormap that have more than 20 options (aka 'tab20'), this method works fine:对于寻找具有 20 多个选项(又名“tab20”)的颜色图的人来说,此方法工作正常:

def generate_colormap(number_of_distinct_colors=100):
    number_of_shades = 7
    number_of_distinct_colors_with_multiply_of_shades = int(math.ceil(number_of_distinct_colors / number_of_shades) * number_of_shades)
    linearly_distributed_nums = np.arange(number_of_distinct_colors_with_multiply_of_shades) / number_of_distinct_colors_with_multiply_of_shades
    arr_by_shade_rows = linearly_distributed_nums.reshape(number_of_shades, number_of_distinct_colors_with_multiply_of_shades // number_of_shades)

    # Transpose the above matrix (columns become rows) - as a result each row contains saw tooth with values slightly higher than row above
    arr_by_shade_columns = arr_by_shade_rows.T

    # Keep number of saw teeth for later
    number_of_partitions = arr_by_shade_columns.shape[0]
    nums_distributed_like_rising_saw = arr_by_shade_columns.reshape(-1)
    initial_cm = hsv(nums_distributed_like_rising_saw)

    lower_partitions_half = number_of_partitions // 2
    upper_partitions_half = number_of_partitions - lower_partitions_half
    lower_half = lower_partitions_half * number_of_shades
    for i in range(3):
        initial_cm[0:lower_half, i] *= np.arange(0.2, 1, 0.8/lower_half)

    # Modify second half in such way that colours towards end of partition are less intense and brighter
    # Colours closer to the middle are affected less, colours closer to the end are affected more
    for i in range(3):
        for j in range(upper_partitions_half):
            modifier = np.ones(number_of_shades) - initial_cm[lower_half + j * number_of_shades: lower_half + (j + 1) * number_of_shades, i]
            modifier = j * modifier / upper_partitions_half
            initial_cm[lower_half + j * number_of_shades: lower_half + (j + 1) * number_of_shades, i] += modifier

    return ListedColormap(initial_cm)

The code was not written by me, but I made some adjustments to improve it.代码不是我写的,但我做了一些调整来改进它。 Sorry, I could not find the original reference link anymore.抱歉,我再也找不到原始参考链接了。

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

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