2016-07-04 3 views
1

분산 형을 채색하기 위해 변수를 사용할 때 어떻게 표현할 수 있습니까? 전설 쇼에 0이라는 라벨이 비어 있고 1이 전체를 나타낼 수있게하려면 어떻게해야합니까?scatterplot에 대한 pyplot 범례 값으로 채색

import matplotlib.pyplot as plt 
X = [1,2,3,1,2,3,4] 
Y = [1,1,1,2,2,2,2] 
label = [0,1,1,0,0,1,1] 
plt.scatter(X, Y, c= label, s=50) 
plt.show() 

답변

2

이 코드를 시도 보내기

import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 
X = [1, 2 ,3, 1, 2, 3, 4] 
Y = [1, 1, 1, 2, 2, 2, 2] 
labels = [0, 1, 1, 0, 0, 1, 1] 
key = {0: ('red', 'empty'), 1: ('green', 'full')} 
plt.scatter(X, Y, c=[key[index][0] for index in labels], s=50) 
patches = [mpatches.Patch(color=color, label=label) for color, label in key.values()] 
plt.legend(handles=patches, labels=[label for _, label in key.values()], bbox_to_anchor=(1, .3)) 
plt.show() 

을 그리고 이것은 당신이 얻을 것이다 무엇 :

enter image description here

색상을 사용하거나 그림에 표시된 것보다 다른 레이블 사전 key의 값을 적절하게 변경하기 만하면됩니다.