如何使用线性回归绘制散点图?

时间:2014-07-24 19:43:19

标签: python graph matplotlib

如何使用matplotlib制作估计的行。

Graph generated using excel

我有几个点,我使用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()

谢谢,

1 个答案:

答案 0 :(得分:0)

使用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()

示例结果:

enter image description here

相关问题