Skip to main content
 首页 » 编程设计

python之相同的符号出现在 Matplotlib 图例中

2024年12月31日10me-sa

我正在 couseera 学习机器学习类(class)。我为数据绘制了一个图并试图制作一个图例。我在网上引用过,用过这种格式

plt.figure(1) 
plt.title('input data plot') 
plt.xlabel('Exam 1 marks') 
plt.ylabel('Exam 2 marks') 
plt.plot(X[temp0,0],X[temp0,1],'or') 
plt.plot(X[temp1,0],X[temp1,1],'xb') 
plt.plot(x_boundary,y_boundary,'-g') 
plt.legend(['Not admitted','Admitted','Decision Boundary'],numpoints=1) 
plt.show() 

所有类型的输出都具有相同的符号

然后我使用下面给出的另一种格式

plt.figure(1) 
plt.title('input data plot') 
plt.xlabel('Exam 1 marks') 
plt.ylabel('Exam 2 marks') 
plt.plot(X[temp0,0],X[temp0,1],'or',label='Not admitted') 
plt.plot(X[temp1,0],X[temp1,1],'xb',label='Admitted') 
plt.plot(x_boundary,y_boundary,'-g',label='Decision Boundary') 
plt.legend() 
plt.show() 

结果是所有数据的图例

谁能帮我解决这个问题。提前谢谢你。

请您参考如下方法:

如果没有更多信息,我不能肯定地说,但我强烈怀疑您的问题出在您的数据上。如果您的值数组包含在另一个数组中,您将获得在第二个示例中看到的行为。

查看此示例以了解不同之处:

import matplotlib.pyplot as plt 
import random 
 
# Note the extra array brackets. This is not good. 
X = [[random.randint(0,10) for i in range(0,5)]] 
Y = [[random.randint(0,10) for i in range(0,5)]] 
 
X1 = [random.randint(0,10)for i in range(0,5)] 
Y1 = [random.randint(0,10)for i in range(0,5)] 
 
plt.plot(X,Y,'or',label='Double Array') 
plt.plot(X1,Y1,'xb',label='Single Array') 
 
plt.legend() 
plt.show()