2016-07-04 10 views
1

散布図の色付けに変数を使用すると、どのような色が表現されているのかを凡例にすることはできますか?凡例を表示するには、0のラベルが空で、1がフルを表すとしますか?散布図の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の値を適切に変更するだけで済みます。

関連する問題