Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A trend line sample in matplotlib Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress
x = np.array([2, 7])
y = np.array([5, 15])
slope, intercept, r_value, p_value, std_err = linregress(x, y)
print("slope: %f, intercept: %f" % (slope, intercept))
print("R-squared: %f" % r_value**2)
slope: 2.000000, intercept: 1.000000
R-squared: 1.000000
plt.figure(figsize=(15, 5))
plt.plot(x, y, 'o', label='original data')
plt.plot(x, intercept + slope*x, 'r', label='fitted line')
plt.legend()
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment