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

PythonScatterTrendline

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3476791View Question on Stackoverflow
Solution 1 - PythonmartinenzingerView Answer on Stackoverflow