Python matplotlib散点';s传说
如您所见,我将Python matplotlib散点';s传说,python,matplotlib,Python,Matplotlib,如您所见,我将颜色设置为标签,以控制函数图例,颜色是一个列表。 问题是运行时,图片中的图例仅显示第一个元素-“黄色” 怎么了? 我希望这幅画的图例能展示这一点的三个要素 我喜欢通过创造三个情节来解决这个问题,虽然有颜色的情节可以一个接一个 from numpy import * import matplotlib import matplotlib.pyplot as plt import matplotlib.patches def file2martix(filename): fr
颜色设置为标签
,以控制函数图例,颜色是一个列表。
问题是运行时,图片中的图例仅显示第一个元素-“黄色”
怎么了?
我希望这幅画的图例能展示这一点的三个要素
我喜欢通过创造三个情节来解决这个问题,虽然有颜色的情节可以一个接一个
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()
请包含datingDataMat
的定义,以便我们可以运行您的代码并亲自查看问题。如果您向我们展示期望的结果和实际结果,这也会有所帮助。看见还要注意,color
变量包含字符串列表,而不是字典。最后,我看到你没有接受任何问题的单一答案,显然也没有对任何问题投赞成票。如果你不奖励回答你的人,答案很快就会停止。你有一个散点图,但有三种颜色的列表。第一个(也是唯一一个)散点图将获得黄色标签,其他标签将被忽略,因为它们没有对应的图。因此,重要的是明确说明预期结果;否则没人能帮你。谢谢你的回答,我知道问题所在。这意味着一个情节,一个标签。我更改了代码。非常感谢。
handlelist = [plt.plot([],color=color)[0] for color in colors]
plt.legend(handlelist,colors,loc=0, ncol=4)