python – 为散点图添加图例
发布时间:2020-12-16 21:30:23 所属栏目:Python 来源:网络整理
导读:这个问题已在SO上提出,但我想找到一个更清晰的解决方案. 给定X是100×2数据,标签是标签向量(从1到9)我绘制散点图如下: pl.scatter(X[:,0],X[:,1],c = labels)pl.show() 如何添加图例来解释一行代码中的颜色?其他解决方案分别绘制每个标签 a = pl.scatter(X
这个问题已在SO上提出,但我想找到一个更清晰的解决方案.
给定X是100×2数据,标签是标签向量(从1到9)我绘制散点图如下: pl.scatter(X[:,0],X[:,1],c = labels) pl.show() 如何添加图例来解释一行代码中的颜色?其他解决方案分别绘制每个标签 a = pl.scatter(X1[:,X1[:,color = "red") b = pl.scatter(X2[:,X2[:,color = "green") c = pl.scatter(X3[:,X3[:,color = "blue") pl.legend((a,b,c),("line 1","line 2","line 3") pl.show() 解决方法
您可以使用所需的图例创建虚拟散点图,如下所示:
pl.scatter(X[:,c = labels) for item in labels: #dummy plot just to create the legend pl.scatter([],[],c = item,label = item) #loc = 0 is for the best position of the legend #scatterpoints = 1 will only show one point in the legend instead of multiple points plt.legend(loc = 0,scatterpoints = 1) pl.show() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |