0
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)
xRandom
  • 69
  • 2
  • 10
  • Please include a definition of `datingDataMat` so we can run your code and see the problem for ourselves. It would also help if you show us the desired result and the actual result. See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Also note that your `color` variable holds a list of strings, not a dictionary. Finally, I see that you have not accepted a single answer to any of your questions and apparently not upvoted any either. If you do not reward those who answer you the answers will soon stop. – Rory Daulton Jul 01 '17 at 10:14
  • You have one scatter plot, but a list of three colors. The first (and only) scatterplot will get the yellow label, other labels will be ignored because there is no corresponding plot to them. It is therefore important to clearly state the desired outcome; otherwise one cannot help you. – ImportanceOfBeingErnest Jul 01 '17 at 10:22
  • Thanks for your answer,i know the problem.Dose it means one plot,one label.And i changed the code.Thanks a lot. – xRandom Jul 01 '17 at 11:35

0 Answers0