Python 如何正确使用离散类的颜色映射(matplotlib)?
Y是一个矩阵。这是在散点图上绘制的Python 如何正确使用离散类的颜色映射(matplotlib)?,python,matplotlib,plot,pca,colormap,Python,Matplotlib,Plot,Pca,Colormap,Y是一个矩阵。这是在散点图上绘制的 Y = [[x1, y1], [x2, y2], ... ] y是y中每个变量的类的列表 classes是所有类的字符串列表 现在,我正在画这样的图: cs = [] cmap = plt.get_cmap(cmap_name) for c in y: for i, l in enumerate(classes): if(str(c) == str(l)): cs.append(i
Y = [[x1, y1],
[x2, y2],
...
]
y
是y中每个变量的类的列表
classes
是所有类的字符串列表
现在,我正在画这样的图:
cs = []
cmap = plt.get_cmap(cmap_name)
for c in y:
for i, l in enumerate(classes):
if(str(c) == str(l)):
cs.append(i)
break;
plt.scatter(Y[:, 0], Y[:, 1])
我还建立了这样一个传奇:
legend_handles = []
for i,l in enumerate(classes):
patch = mpatches.Patch(color=cmap(i/len(classes)), label=l)
legend_handles.append(patch)
plt.legend(handles=legend_handles, prop={'size':10})
然而,这个传说并不正确。如何使用图例正确设置此选项 您可以立即为
pyplot.scatter添加标签:plt.scatter(…,label=…)
,您只需选择与所讨论的类相对应的值(即,知道与类相对应的索引,然后执行Y[索引,0]
),谢谢。工作完美。您可以立即为pyplot.scatter
添加标签:plt.scatter(…,label=…)
,您只需选择与所讨论的类相对应的值(即知道与类相对应的索引,然后执行Y[索引,0]
),谢谢。工作得很好。