How to make scatter plot with trendline and stats in python
Автор: Karina Adcock
Загружено: 20 сент. 2023 г.
Просмотров: 7 858 просмотров
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()

Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: