t-sne散点图与传说

问题描述 投票:0回答:1

我的代码

X:没有答案的数据集

y:回答(0,1,2或3)

%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
X_reduced = TSNE(n_components=2, perplexity=113.0,random_state=0).fit_transform(X)
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap='Greens')
plt.legend(["A","B","C","D"], loc='best')

然后我得到了this

但我希望“A,B,C,D”的“传说”对应每种颜色(浅绿色到深绿色)

如果你能回答这个问题我会很感激。

matplotlib plot legend scatter
1个回答
0
投票

如果y表示一个类别。然后最简单的方法是循环y的不同值,并在传递标签时用标准plt.plot绘制点:

# make a mapping from category to your favourite colors and labels
category_to_color = {0: 'lightgreen', 1: 'lawngreen', 2:'limegreen', 3: 'darkgreen'}
category_to_label = {0: 'A', 1:'B', 2:'C', 3:'D'}

# plot each category with a distinct label
fig, ax = plt.subplots(1,1)
for category, color in category_to_color.items():
    mask = y == category
    ax.plot(X_reduced[mask, 0], X_reduced[mask, 1], 'o', 
            color=color, label=category_to_label[category])

ax.legend(loc='best')
© www.soinside.com 2019 - 2024. All rights reserved.