Created
September 29, 2020 02:55
-
-
Save yuyasugano/f2a51fca90049f67aa8aeb1c0c61bcab to your computer and use it in GitHub Desktop.
A trend line sample in matplotlib Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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