How to make scatter plot with trendline and stats in python

Описание к видео How to make scatter plot with trendline and stats in python

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\nR$^2$=%.3f\np=%.3f"%(slope,intercept,rvalue**2,pvalue),xy=(0.15,0.7),xycoords='figure fraction')

plt.show()

Комментарии

Информация по комментариям в разработке