我正在尝试使用matplotlib使用误差线进行线性回归。我不知道如何添加错误栏。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
x = [6, 15, 24, 33, 41, 52, 59, 66, 73, 81]
y = [5,10,15,20,25,30,35,40,45,50]
coef = np.polyfit(x,y,1)
poly1d_fn = np.poly1d(coef) #to create a linear function with coefficients
plt.plot(x,y,'yo', x, poly1d_fn(x), '-k')
plt.show()
答案 0 :(得分:1)
也许是这样吗?
import matplotlib.pyplot as plt
import numpy as np
x = np.array([6, 15, 24, 33, 41, 52, 59, 66, 73, 81])
y = np.array([5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
coef = np.polyfit(x, y, 1)
poly1d_fn = np.poly1d(coef) # to create a linear function with coefficients
plt.plot(x, y, 'ro', x, poly1d_fn(x), '-b')
plt.errorbar(x, poly1d_fn(x), yerr=poly1d_fn(x) - y, fmt='.k')
plt.show()