我的代码
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”的“传说”对应每种颜色(浅绿色到深绿色)
如果你能回答这个问题我会很感激。
如果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')