Reputation: 63
I have data from dataFrame plotted that looks like this Image now i need to add a regression line that fits to existing plot.
What is the simplest way to add regression line?
the code used is this:
df2 = myReport.loc[:, ['Quantity']]
print(df2)
plt.plot(df2, linestyle="", marker="." )
plt.axis([0,45,5023203,5938119])
plt.grid(True)
plt.legend(df2.columns)
plt.ylabel ('Population')
plt.xlabel ('Timeline')
plt.show()
Using python version 2.7.
Upvotes: 3
Views: 148
Reputation: 7404
Try
from scipy.stats import linregress
X = np.arange(length(df2))
y = df2
slope, intercept, r_value, p_value, std_err = linregress(X,y)
x = np.linspace(x.min(), x.max(),1001)
plt.plot(x, slope*x+intercept)
Upvotes: 1