我怎样才能使用 matplotlib 做出这样的估计线。
我有几个点,我使用以下代码使用 matplotlib 绘制了它们:
import matplotlib.pyplot as plt
for smp, lbl in zip(samples, labels):
plt.scatter(smp[0], smp[1], marker='*', cl = 'b', s=100, label=lbl)
# set limit, xlabel, ylabel, legend ...
# ...
plt.show()
谢谢,
最佳答案
使用polyfit
做线性回归:
import matplotlib.pyplot as plt
from pylab import polyfit, poly1d
x, y = zip(*samples)
fit = polyfit(x, y, 1)
fit_fn = poly1d(fit)
plt.plot(x,y, '*', x, fit_fn(x), 'k')
plt.show()
示例结果:
关于python - 如何用线性回归绘制散点图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24942631/