2016-09-27 75 views
0

私は以下のように行列をプロットしており、凡例が何度も繰り返しています。私はnumpoints = 1を使ってみましたが、これは何の効果もないようです。何かヒント?Python - 凡例の値が重複しています

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 
import matplotlib 
%matplotlib inline 
matplotlib.rcParams['figure.figsize'] = (10, 8) # set default figure size, 8in by 6inimport numpy as np 

data = pd.read_csv('data/assg-03-data.csv', names=['exam1', 'exam2', 'admitted']) 

x = data[['exam1', 'exam2']].as_matrix() 
y = data.admitted.as_matrix() 

# plot the visualization of the exam scores here 
no_admit = np.where(y == 0) 
admit = np.where(y == 1) 
from pylab import * 
# plot the example figure 
plt.figure() 
# plot the points in our two categories, y=0 and y=1, using markers to indicated 
# the category or output 
plt.plot(x[no_admit,0], x[no_admit,1],'yo', label = 'Not admitted', markersize=8, markeredgewidth=1) 
plt.plot(x[admit,0], x[admit,1], 'r^', label = 'Admitted', markersize=8, markeredgewidth=1) 
# add some labels and titles 
plt.xlabel('$Exam 1 score$') 
plt.ylabel('$Exam 2 score$') 
plt.title('Admit/No Admit as a function of Exam Scores') 
plt.legend() 
+0

(行)毎回。彼らはまったく同じ色とシンボルになります。 – Evert

+0

おそらく、各タイプの1つのデータセットをプロットし、それらにラベルを割り当て、ラベルなしで各タイプの残りのデータセットをプロットすることができます。これは、 'admit'または' no_admit'が空であるか、その最初のデータセットに対してのみ有効であるときに問題になる可能性があります。 – Evert

答えて

0

特に、パンダに慣れていない場合は、データ形式の例を入れないとこの問題を理解することはほとんど不可能です。 しかし、あなたの入力を仮定すると、この形式になっています。

x=pd.DataFrame(np.array([np.arange(10),np.arange(10)**2]).T,columns=['exam1','exam2']).as_matrix() 
y=pd.DataFrame(np.arange(10)%2).as_matrix() 

>>x 
array([[ 0, 0], 
    [ 1, 1], 
    [ 2, 4], 
    [ 3, 9], 
    [ 4, 16], 
    [ 5, 25], 
    [ 6, 36], 
    [ 7, 49], 
    [ 8, 64], 
    [ 9, 81]]) 

>> y 
array([[0], 
    [1], 
    [0], 
    [1], 
    [0], 
    [1], 
    [0], 
    [1], 
    [0], 
    [1]]) 

理由は行列にデータフレームから奇妙な変換であり、私はあなたがベクトル(1D配列)を持っている場合、それは起こらないだろうと思います。この作品私の例えば (xyための2D行列はどこから来るのか、それはきれいな形であればわからない、私は知らない):

あなたは複数のデータセットをプロットしているので、驚くことではありません
plt.plot(x[no_admit,0][0], x[no_admit,1][0],'yo', label = 'Not admitted', markersize=8, markeredgewidth=1) 
plt.plot(x[admit,0][0], x[admit,1][0], 'r^', label = 'Admitted', markersize=8, markeredgewidth=1) 
+0

それはうまくいった!あなたの助けをありがとう! – user3399935

関連する問題