Python轨迹图对比如何实现?

请教一个问题,有两个轨迹图,都是经纬度画的轨迹,怎么比较两个轨迹图上每个点的轨迹偏差呢?


Python轨迹图对比如何实现?
7 回复

这事其实和 python 没啥关系


要对比两个轨迹图,最直接的方法是用matplotlib把它们画在同一张图上。给你个实用的例子:

import matplotlib.pyplot as plt
import numpy as np

# 生成示例轨迹数据
np.random.seed(42)
n_points = 50

# 轨迹1:带噪声的直线
x1 = np.linspace(0, 10, n_points)
y1 = 2 * x1 + np.random.normal(0, 1, n_points)

# 轨迹2:带噪声的曲线
x2 = np.linspace(0, 10, n_points)
y2 = x2 + 0.1 * x2**2 + np.random.normal(0, 2, n_points)

# 创建对比图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))

# 子图1:轨迹叠加对比
ax1.plot(x1, y1, 'b-', linewidth=2, label='轨迹1 (线性)')
ax1.plot(x2, y2, 'r--', linewidth=2, label='轨迹2 (二次)')
ax1.set_xlabel('X坐标')
ax1.set_ylabel('Y坐标')
ax1.set_title('轨迹叠加对比')
ax1.legend()
ax1.grid(True, alpha=0.3)

# 子图2:轨迹差异可视化
ax2.scatter(x1, y1, c='blue', s=30, alpha=0.6, label='轨迹1点')
ax2.scatter(x2, y2, c='red', s=30, alpha=0.6, label='轨迹2点')

# 连接对应点显示差异
for i in range(0, n_points, 5):  # 每5个点画一条连接线
    ax2.plot([x1[i], x2[i]], [y1[i], y2[i]], 'k-', alpha=0.3, linewidth=0.5)

ax2.set_xlabel('X坐标')
ax2.set_ylabel('Y坐标')
ax2.set_title('轨迹点差异对比')
ax2.legend()
ax2.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

# 计算轨迹差异指标
def calculate_trajectory_diff(x1, y1, x2, y2):
    """计算两条轨迹的差异指标"""
    # 欧氏距离差异
    distances = np.sqrt((x1 - x2)**2 + (y1 - y2)**2)
    
    # 方向差异(角度差)
    dx1 = np.diff(x1)
    dy1 = np.diff(y1)
    dx2 = np.diff(x2)
    dy2 = np.diff(y2)
    
    angles1 = np.arctan2(dy1, dx1)
    angles2 = np.arctan2(dy2, dx2)
    angle_diff = np.abs(angles1 - angles2)
    
    return {
        '平均距离差': np.mean(distances),
        '最大距离差': np.max(distances),
        '平均角度差(度)': np.mean(np.degrees(angle_diff))
    }

# 计算并显示差异
diff_metrics = calculate_trajectory_diff(x1, y1, x2, y2)
print("轨迹差异指标:")
for key, value in diff_metrics.items():
    print(f"{key}: {value:.4f}")

这段代码做了几件事:

  1. 生成两条示例轨迹(一条直线,一条曲线)
  2. 左边子图直接叠加显示,用不同颜色和线型区分
  3. 右边子图用散点+连接线的方式直观展示对应点的差异
  4. 最后计算了几个关键差异指标:平均距离差、最大距离差和方向角度差

如果你的轨迹数据来自文件,只需要替换x1,y1,x2,y2为你的实际数据就行。想要更高级的对比,比如动态轨迹对比或者3D轨迹,可以基于这个框架扩展。

总结:用matplotlib叠加绘制加差异计算最直接。

确定每个点的坐标先?

每个点的坐标应该能拿到,都是 gpx 文件

那同时遍历两个轨迹的点,求偏差不就好了 m

时间序列不一致,不好一一对应上

首先应该都投影到同一个空间地理坐标系里吧,以前用 arcpy 好像是有一些函数(时间有点久记不太清),不过这玩意太重了。或者看看有没有啥专门处理地理信息的库?

回到顶部