How could I add trendline to a dot graph drawn using matplotlib.scatter?
Add a Trendline With NumPy in Python Matplotlib We calculate the trendline with NumPy . To do that, we need the x- and y-axis. Then we use the polyfit and poly1d functions of NumPy . And finally, we plot the trendline.
To create a polynomial trendline, simply change the value in the np. polyfit() function.
as explained here
With help from numpy one can calculate for example a linear fitting.
# 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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With