matplotlib画散点图十分的方便,使用numpy的polyfit函数实现Excel中的趋势线功能也是很简单的。API参考地址
# plot the data itself
pylab.plot(x,y,‘o’)
# calc the trendline (it is simply a linear fitting)
z = numpy.polyfit(x, y, 1)
p = numpy.poly1d(z)
pylab.plot(x,p(x),“r–”)
# the line equation:
print “y=%.6fx+(%.6f)”%(z[0],z[1])
这是线性趋势线,二阶的如下,其他不变
z = numpy.polyfit(x, y, 2)
本文介绍了如何利用matplotlib和numpy在散点图中添加趋势线,特别提到了线性趋势线和二阶趋势线的实现方法,参照了官方API。

1818

被折叠的 条评论
为什么被折叠?



