Рет қаралды 5,361
Get a chart with a linear regression line of best fit and the equation of the line, the r-squared value and the p-value.
---------------------------------------------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
x = np.array([2,4,5,7,10])
y = np.array([1,5,4,8,11])
plt.scatter(x,y)
slope, intercept, rvalue, pvalue, stderr = stats.linregress(x,y)
plt.plot(x,slope*x+intercept)
plt.annotate("y=%.3fx+%.3f
R$^2$=%.3f
p=%.3f"%(slope,intercept,rvalue**2,pvalue),xy=(0.15,0.7),xycoords='figure fraction')
plt.show()