Python - legend values duplicate
<p>I'm plotting a matrix, as shown below, and the legend repeats over and over again.  I've tried using numpoints = 1 and this didn't seem to have any effect.  Any hints?</p>

<pre><code>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()
</code></pre>

<p>It's nearly impossible to understand the problem if you don't put an example of data format especially if one is not familiar with pandas.
However, assuming your input has this format:</p>

<pre><code>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()

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

&gt;&gt; y
array([[0],
   [1],
   [0],
   [1],
   [0],
   [1],
   [0],
   [1],
   [0],
   [1]])
</code></pre>

<p>the reason is the strange transformation from DataFrame to matrix, I guess it wouldn't happen if you have vectors (1D arrays).
For my example this works (not sure if it is the cleanest form, I don't know where the 2D matrix for <code>x</code> and <code>y</code> comes from):</p>

<pre><code>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) 
</code></pre>

python matplotlib legend