Skip to main content
 首页 » 编程设计

python之在 python 中绘制点集

2025年01月19日11daizhj

我正在尝试绘制一组测试点 VS 正在使用 Python 测试的点。我尝试了几个代码,但无法弄明白。

train_pts2 = { 
   "N": [(0.125,0.11), (0.375,0.21), (0.625,0.31), (0,0.01), (0.375,0.50), (0.80,0)], 
   "Y": [(0.075,0.38), (0.5,0.22), (1,0.41), (0.70,1),(0.325,0.65), (0.70,0.61)], 
   "TBD": [(0.70,0.61)] 
   } 

因此该图将是散点图,表明 pLoan 属于 N 还是 Y

有点像

请您参考如下方法:

您可以尝试使用 python 中的 matplotlib 库和 scatter() 函数

import numpy as np 
import matplotlib.pyplot as plt 
 
train_pts2 = { 
   "N": [(0.125,0.11), (0.375,0.21), (0.625,0.31), (0,0.01), (0.375,0.50), (0.80,0)], 
   "Y": [(0.075,0.38), (0.5,0.22), (1,0.41), (0.70,1),(0.325,0.65), (0.70,0.61)], 
   "TBD": [(0.70,0.61)] 
   } 
 
colors = { 
    "N" : "orange", 
    "Y" : "blue", 
    "TBD" : "green" 
} 
 
for label in ["N", "Y", "TBD"]: 
    x = [item[0] for item in train_pts2[label]] 
    y = [item[1] for item in train_pts2[label]] 
    plt.scatter(x, y, c=colors[label], label=label) 
 
plt.xlabel("Age of Loan") 
plt.ylabel("Loan Amount") 
plt.xlim(xmin=0) 
plt.ylim(ymin=0) 
plt.grid(True) 
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) 
plt.show() 

欲了解更多信息,您可以调用scatter_demo.py示例。

希望对您有所帮助。