from numpy import *
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches
def file2martix(filename):
fr = open(filename)
arryOfLines = fr.readlines()
numbersOfLines = len(arryOfLines)
print(numbersOfLines)
returnMat = zeros((numbersOfLines,3))
print(returnMat)
classLableVector = []
index = 0
for line in arryOfLines:
line = line.strip()
listFromLine = line.split('\t')
returnMat[index,:] = listFromLine[0:3]
print(listFromLine[0:3])
classLableVector.append(int(listFromLine[-1]))
index = index + 1
return returnMat,classLableVector
datingDataMat,datingLables = file2martix('1.txt')
fig = plt.figure()
ax = fig.add_subplot(111)
color = ['yellow','green','blue']
ax.scatter(datingDataMat[:,1],datingDataMat[:,2],s = 15,c = color)
print(datingLables)
plt.legend(color[0:2], loc=0, ncol=4)
plt.show()
As you see,i set the color
as the label
to control the function-legend,color is a list.
The problem is when is run,the legend in the picture only show the first element-'yellow'
What's wrong with it?
I hope the picture's legend can show the three elements about the point
I love the problem by creat three plot,though the plot with the color can one by one .
handlelist = [plt.plot([],color=color)[0] for color in colors]
plt.legend(handlelist,colors,loc=0, ncol=4)