How to make scatter plot with trendline and stats in python

preview_player
Показать описание
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

from scipy import stats

Рекомендации по теме
Комментарии
Автор

If you are doing this with dates, scipy can't handle dates so you have to use:
Import matplotlib.dates as mdates
new_x = mdates.date2num(x)
To covert your dates into numbers.
Then the slope will be in units of days.

karinaadcock
Автор

Thanks.. gave a really nice process of construction.. got me plaing for a few hours after watching and here is my update:
def plot_regression(x, y, x_label='X', y_label='Y', title='Linear Regression'):
plt.scatter(x, y, c='black', marker='x', label='Data Points', alpha=0.7, s=20)
# Linear regression with dark blue line and narrower width
slope, intercept, rvalue, pvalue, stderr = stats.linregress(x, y)
plt.plot(x, slope * x + intercept, c='darkblue', label='Regression Line', linewidth=1)
quantile_values = np.arange(1, 101)
x_quantiles = np.percentile(x, quantile_values)
y_quantiles = np.percentile(y, quantile_values)
plt.scatter(x_quantiles, y_quantiles, c='red', marker='x', s=20, label='Quantiles')
# Annotation
annotation_text = (f"y = {slope:.3f}x + {intercept:.3f}\n"
f"R$^2$ = {rvalue**2:.3f}\n"
f"p = {pvalue:.3f}")
plt.annotate(annotation_text, xy=(0.15, 0.7), xycoords='axes fraction', fontsize=10,
bbox=dict(facecolor='white', edgecolor='black', boxstyle='square, pad=0.5'))
# Labels, title, and legend
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)
# Position legend outside the plot area to the right
plt.legend(loc='upper left', bbox_to_anchor=(1, 1))
# Add light grid lines
plt.grid(True, which='both', linestyle='--', linewidth=0.5, color='grey', alpha=0.5)
# Adjust layout to accommodate the legend
plt.tight_layout(rect=[0, 0, 0.85, 1])
# Show plot
plt.show()

Oceansteve
Автор

How can I do this for a log - log plot?

itsame