2016-09-27 80 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

您可能可以繪製每種類型的一個數據集,爲這些數據集分配一個標籤,併爲每種類型繪製沒有標籤的剩餘數據集。當「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]]) 

的原因是從數據框中陌生轉變爲矩陣,我想如果你有向量(一維數組),這將不會發生。 在我的例子這工作(不知道這是最乾淨的形式,我不知道在哪裏二維矩陣xy來自):

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