作图:plt.scatter() “x and y must be the same size“

目的:绘制真实值-预测值的散点图,横坐标为“样本编号”, 纵坐标为“y值”

预期图像:

Bug描述:

def scatter_true_pred(y_true, y_predict):
    num_test = y_true.shape[0]
    plt.figure(figsize=(30, 10), dpi= 200, facecolor='w', edgecolor='k')
    plt.xticks(fontsize=12);plt.yticks(fontsize=12)
    plt.xlabel("样本编号", fontsize=20);plt.ylabel("翘曲值(归一化后)", fontsize=20)
    plt.title("预测值与测试样本",fontsize=30)
    print("num_test", num_test, "num_y_true", y_true.shape[0])
    true_point = plt.scatter(num_test, y_true, color='blue', marker='o')
    pre_point = plt.scatter(num_test, y_predict, color='green', marker='^')
    #添加图例
    plt.legend((true_point, pre_point), ("真实值","预测值"), loc='upper left')

这行 num_test = y_true.shape[0]  中的num_test的类型是一个单独的整数,无法对应所有的样本编号, scatter 函数要求传入一个数组, 如ndarray

修改方案:

#将数字转化为数组,该数组包含从1~最大样本编号的所有值
num_test = np.arange(1, y_predict.shape[0]+1, 1)

注意!!!   arange(起步值, 最终值, 步长) 是左闭右开的, 返回值类型是ndarray

test_arange = np.arange(1, 10, 1)
test_arange, type(test_arange)
#(array([1, 2, 3, 4, 5, 6, 7, 8, 9]), numpy.ndarray)

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值