python - 如何自定义 matplotlib 中的误差线
问题描述
我打算在我的图中绘制所有可能的信息,包括平均值、标准差和 MSE,并参考图中的每个点。
from sklearn.metrics import mean_absolute_error as mae
from sklearn.metrics import mean_squared_error as mse
import numpy as np
import matplotlib.pyplot as plt
为了简单起见,假设我只有三点。
true = np.array([[1047.]
[ 953.]
[1073.]])
pred = np.array([[ -69.921265]
[-907.8611 ]
[ 208.98877 ]])
my_mae= mae(true, pred) #mean absolute error
my_mse= mse(true,pred) #mean squared error
err = abs(true - pred) #get the error per point
mean_err = np.mean(err) #calculate the mean
sd_err = np.std(err) #calculate the standard deviation
然后,我绘制我的错误栏。
dy= 100
plt.errorbar(true,pred, yerr=dy, fmt='o', color='black',ecolor='red', elinewidth=3, capsize=0);
首先,我想以某种方式引用每个错误栏以查看它引用的数据点。其次,我想将所有四条信息添加到情节中。我会很感激任何帮助。
解决方案
在这里,如果这解决了您的问题,请考虑接受答案:
from sklearn.metrics import mean_absolute_error as mae
from sklearn.metrics import mean_squared_error as mse
import numpy as np
import matplotlib.pyplot as plt
true = np.array([[1047.],
[ 953.],
[1073.]])
pred = np.array([[ -69.921265],
[-907.8611 ],
[ 208.98877 ]])
my_mae= mae(true, pred) #mean absolute error
my_mse= mse(true,pred) #mean squared error
err = abs(true - pred) #get the error per point
mean_err = np.mean(err) #calculate the mean
sd_err = np.std(err) #calculate the standard deviation
dy= 100
for i, z in enumerate (pred,1):
plt.errorbar(true,pred, yerr=dy, fmt='o', color='black',ecolor='red', elinewidth=3, capsize=0, zorder=3);
plt.annotate(i, (true[i-1], pred[i-1]),fontsize=20, color='blue')
label_1=['my_mae','my_mse', 'mean_err', 'sd_err']
label_2=[my_mae,my_mse, mean_err,sd_err]
for q,w in zip(label_1, label_2):
plt.plot([], [],'o', label=(f'{q}: {w}'))
plt.legend(loc='lower right')
这是你应该得到的:
推荐阅读
- javascript - Moment isValid 方法不断返回弃用警告
- r - 创建数据框列表 => 警告:在 df_list[i] <- as.data.frame(df_ex) 中:要替换的项目数不是替换长度的倍数
- python - 构建 Python 扩展失败(“kernel32.lib”无法打开 |“x64”与目标机器类型“x86”冲突)
- javascript - 通过原型扩展 JavaScript 中的对象功能
- scala - 在任意 scala 对象中调用私有方法
- docker - 与 docker-compose.yml 一起使用时未执行 Dockerfile 中的命令
- javascript - 像时刻一样从对象格式化 Luxon 日期
- javascript - 有没有办法使用 Puppeteer 定位特定元素并在将 html 转换为 pdf 时保留 CSS?
- python - 将库导入 Python 文件
- postgresql - postgresql/pgAdmin - 接受 start_date 和 end_date 参数作为查询运行的输入