如何在python matplotlib点(散点)图中添加trendline?

时间:2023-02-02 07:38:40

How could I add trendline to a dot graph drawn using matplotlib.scatter?

如何将趋势线添加到使用matplotlib.scatter绘制的点图中?

1 个解决方案

#1


39  

as explained here

如此处所述

With help from numpy one can calculate for example a linear fitting.

在numpy的帮助下,可以计算出例如线性拟合。

# plot the data itself
pylab.plot(x,y,'o')

# calc the trendline
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])

#1


39  

as explained here

如此处所述

With help from numpy one can calculate for example a linear fitting.

在numpy的帮助下,可以计算出例如线性拟合。

# plot the data itself
pylab.plot(x,y,'o')

# calc the trendline
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])