首页 > 解决方案 > 用完全不同的颜色绘制散点图

问题描述

我正在尝试创建一个散点图,但是,我得到了不同类的重复颜色(我有 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')

我尝试在第一个 pl.scatter() 调用中添加一些颜色图(例如:cmap='bwr'),但这并不能解决我的问题。

我的 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 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 ...]

这是我目前的情节:

当前地块

有没有人知道如何将 c 参数保持为预测类,但有不同的颜色可以让我更好地可视化它?

标签: pythonmatplotlibscikit-learn

解决方案


对于寻找具有 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)

代码不是我写的,但我做了一些调整来改进它。抱歉,我再也找不到原来的参考链接了。


推荐阅读