Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created September 29, 2020 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuyasugano/f2a51fca90049f67aa8aeb1c0c61bcab to your computer and use it in GitHub Desktop.
Save yuyasugano/f2a51fca90049f67aa8aeb1c0c61bcab to your computer and use it in GitHub Desktop.
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