add trendline to plot matplotlib

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

Here is what the above code is Doing:
1. We’re using the polyfit function from numpy to fit a straight line to our data.
2. We’re using the poly1d function from numpy to create a function that we can use to calculate the y-values of the trendline based on the x-values of the data.
3. We’re using the plot function from pylab to plot the data itself.
4. We’re using the plot function from pylab to plot the trendline.
5. We’re printing the equation of the trendline.