给定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()@R_419_6120@ 您可以使用所需的图例创建虚拟散点图,如下所示:
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 pointsplt.legend(loc = 0,scatterpoints = 1)pl.show()总结
以上是内存溢出为你收集整理的python – 为散点图添加图例全部内容,希望文章能够帮你解决python – 为散点图添加图例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)