Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add trendline in python matplotlib dot (scatter) graphs?

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

like image 674
user3476791 Avatar asked Oct 19 '14 04:10

user3476791


People also ask

How do you add a trendline in Matplotlib?

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.

What NumPy function do you use to add a trend line to a scatter plot?

To create a polynomial trendline, simply change the value in the np. polyfit() function.


1 Answers

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])
like image 71
martinenzinger Avatar answered Oct 03 '22 11:10

martinenzinger