2014-04-15 1 views
4

이 질문은 SO에 대한 질문이지만 명확한 해결책을 찾고 싶습니다.분산 그림에 대한 범례 추가

나는 다음과 같은 산란 플롯을 플롯

감안할 때 X는 데이터를 100x2되며, 레이블 (9-1)에서 레이블 벡터입니다 : 색상을 설명하는 범례를 추가하는 방법

pl.scatter(X[:,0], X[:,1], c = labels) 
pl.show() 

enter image description here

단 한 줄의 코드에서? 다른 솔루션은 개별적으로 각 레이블을 플롯 :

a = pl.scatter(X1[:,0], X1[:,1], color = "red") 
b = pl.scatter(X2[:,0], X2[:,1], color = "green") 
c = pl.scatter(X3[:,0], X3[:,1], color = "blue") 
pl.legend((a,b,c), ("line 1", "line 2", "line 3") 
pl.show() 
+1

나는 당신이 아마 본 적이 있지만, 경우에 당신이 모르는 : http://stackoverflow.com/questions/17411940/matplotlib-scatter-plot-legend – s16h

+0

그냥 각 플롯 레이블과 전설()를 호출 : plt.scatter (x1, y1, label = str (pointset1)); ...; plt.legend (loc = '우측 상단', numpoints = 1, ncol = 3, fontsize = 8, bbox_to_anchor = (1,1)); plt.show() – Ralf

답변

0

그냥 각 플롯 레이블을하고

plt.scatter(x1,y1,label=str(pointset1)) 
plt.scatter(x2,y2,label=str(pointset2)) 
plt.scatter(x3,y3,label=str(pointset3)) 

plt.legend(loc='upper right', numpoints=1, ncol=3, fontsize=8, bbox_to_anchor=(1,1)) 
plt.show() 
0

다음과 같이 원하는 전설 더미 산포도를 만들 수 있습니다 :)처럼) (전설을 호출 :

pl.scatter(X[:,0], X[:,1], 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()