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()
這並不奇怪,因爲你繪製多個數據集(線)每次;他們恰好是相同的顏色和符號。 – Evert
您可能可以繪製每種類型的一個數據集,爲這些數據集分配一個標籤,併爲每種類型繪製沒有標籤的剩餘數據集。當「admit」或「no_admit」爲空或僅對第一個數據集有效時,這可能會出現問題。 – Evert