【发布时间】:2016-06-19 22:07:35
【问题描述】:
我需要计算一条线与水平线之间的角度。我的高中数学似乎不及格。
import matplotlib.pyplot as plt
import numpy as np
x = [8450.0, 8061.0, 7524.0, 7180.0, 8247.0, 8929.0, 8896.0, 9736.0, 9658.0, 9592.0]
y = range(len(x))
best_fit_line = np.poly1d(np.polyfit(y, x, 1))(y)
slope = (y[-1] - y[0]) / (x[-1] - x[0])
angle = np.arctan(slope)
print 'slope: ' + str(slope)
print 'angle: ' + str(angle)
plt.figure(figsize=(8,6))
plt.plot(x)
plt.plot(best_fit_line, '--', color='r')
plt.show()
结果如下:
slope: 0.00788091068301
angle: 0.00788074753125
我需要水平线和红色虚线之间的角度。光看它,它可能应该在 30-45 度之间。我做错了什么?
* 关于slope = (y[-1] - y[0]) / (x[-1] - x[0]),我也尝试过numpy.diff 和scipy.stats.linregress,但也没有成功。
【问题讨论】:
-
您忘记考虑图表的比例。
-
另外,请使用arctan2!
-
嗨 jtbandes,我如何计算图表的比例?
标签: python numpy math time-series angle