python - 使用坐标计算 numpy 角度
问题描述
我必须计算两点之间的角度,比如 A (x1, y1) 和 B (x2, y2)。我正在使用的当前代码如下 -
import math
direction = math.degrees(math.atan((y2 - y1) / (x2 - x1)))
我尝试使用以下 numpy 代码执行相同的代码-
x = np.asarray(data['x'])
y = np.asarray(data['y'])
direction = np.rad2deg(np.arctan2(y, x))
这里,“x”和“y”指的是具有坐标的两个属性。
但是,我使用 numpy 获得的方向计算值与使用“math”包获得的计算值不同。
只是为了提供最小值和最大值-
data['x'].min(), data['x'].max()
# (25.24, 803.85)
data['y'].min(), data['y'].max()
# (21.44, 805.76)
“x”和“y”的两个分布几乎呈正态分布。
如何使用 numpy 实现角度计算?
谢谢!
样本数据-
data_d =
{'time': [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
'x': [405.31,
405.3,
405.29,
405.27,
405.27,
405.27,
405.31,
405.38,
405.46,
405.54,
405.63],
'y': [417.07,
416.86,
416.71,
416.61,
416.54,
416.49,
416.37,
416.27,
416.13,
415.93,
415.84],
'direction': [87.27368900609596,
86.18592516571398,
78.69006752595475,
0.0,
0.0,
-71.56505117706985,
-55.007979801450084,
-60.25511870306028,
-68.19859051363696,
-45.00000000001809,
-67.38013505194608],
'direction_np_computation': [-134.1807285626706,
-134.19444442862144,
-134.2040441491018,
-134.2095039258752,
-134.21431600901414,
-134.2177537199761,
-134.2288323248442,
-134.24065659640502,
-134.2559407971113,
-134.27535831135458,
-134.2879109537407]}
data = pd.DataFrame(data_d)
在这里,“direction”列是使用“math”包计算的,“direction_np_computation”是使用代码计算的——
# Reference point- Xr = 0 Yr = 0
# Get 'x' and 'y' attributes from 'data'- x = np.array(data['x']) y = np.array(data['y'])
# Compute direction from reference point with the coordinates- direction = np.rad2deg(np.arctan2(Yr - y, Xr - x))
为什么列之间不匹配?
解决方案
在我看来,它工作正常。由于我没有看到你的字符 numpy/pandas 坐标数组,我不能给你确切的解决方案
3个版本:
反正切
>>> direction = np.rad2deg(np.arctan((2-1)/(2-1)))
>>> direction
45.0
数学
>>> direction = math.degrees(math.atan((2 - 1) / (2 - 1)))
>>> direction
45.0
arctan2
>>> direction = np.rad2deg(np.arctan2((2-1),(2-1)))
>>> direction
45.0
使用假数据(因为我没有看到您的数据),使用您的功能,
参照点:
>>> Xr = 10
>>> Yr = 10
使用这些坐标检查参考点的方向:
>>> Xs = np.array([1,2,3,4,5,6])
>>> Ys = np.array([1,2,3,4,5,6])
我希望它们都有 45 度的方向
>>> direction = np.rad2deg(np.arctan2(Yr-Ys,Xr-Xs))
>>> direction
array([45., 45., 45., 45., 45., 45.])
编辑
根据您提供的数据:
看来您对结果不同的概念是正确的。
更多内容在这里:numpy arctan2 错误或使用问题?
使用 np.arctan 应该没问题
numpy 模块
>>> direction = np.rad2deg(np.arctan(data.y/data.x))
>>> direction
0 45.819271
1 45.805556
2 45.795956
3 45.790496
4 45.785684
5 45.782246
6 45.771168
7 45.759343
8 45.744059
9 45.724642
10 45.712089
dtype: float64
数学模块
>>> for i in range(10):
... math.degrees(math.atan((data.y[i]) / (data.x[i])))
...
45.81927143732942
45.805555571378584
45.79595585089821
45.79049607412479
45.78568399098587
45.782246280023934
45.771167675155795
45.75934340359498
45.744059202888714
45.72464168864543
>>>
笔记
小心符号+-。Y2-Y1/X2-X1 vs Y1-Y2/X1-X2 可以给出不同的结果,但实际上两者都是正确的。在我们的例子中,这些结果是 45 度或 -135 度。两个都是正确的,只有一个是顺时针的
推荐阅读
- javascript - 如何在 SSR React 应用程序中以编程方式获取我的 Google 优化实验 ID?
- javascript - 创建 ms 团队应用程序中的 javascript 实现问题
- javascript - 没有得到第三个条件
- spring - 为什么这个条件批处理流包含在spring批处理的无限循环中?
- c++ - C++中的基本类继承
- excel - 如果值包含未转义的逗号,是否有一种简单的方法来解析 Excel、Power Query 或 VBA 中逗号分隔的键:值对?
- plot - Gnuplot 数据符合日志函数
- python - 在 Pandas 中创建多索引标头
- angular - 适用于 Visual Studio Code 的 Microsoft Edge 开发人员工具不会自动将调试器附加到 Angular 项目
- python - 如何根据外键列出所有产品?