如何用Python可视化Kedarkantha徒步GPX文件的海拔与路线数据?
生成Kedarkantha徒步路线海拔剖面图(Elevation vs Distance)
我用GPS追踪应用记录了Kedarkantha徒步行程,该应用导出了一个.gpx文件,希望创建一个展示海拔 vs 距离的折线图,可视化徒步路线的剖面图。
以下是实现代码:
import gpxpy import matplotlib.pyplot as plt with open("kedarkantha.gpx", "r") as f: gpx = gpxpy.parse(f) elevations = [] distances = [] for track in gpx.tracks: for segment in track.segments: prev_point = None total_distance = 0 for point in segment.points: if prev_point: # 计算当前点与上一点的3D距离并累加 total_distance += point.distance_3d(prev_point) elevations.append(point.elevation) distances.append(total_distance) prev_point = point # 绘制海拔剖面图 plt.plot(distances, elevations) plt.xlabel("距离 (米)") plt.ylabel("海拔 (米)") plt.title("Kedarkantha徒步路线海拔剖面图") plt.show()
代码说明:
- 使用
gpxpy库读取并解析GPX文件,提取轨迹中的所有点位信息 - 遍历轨迹分段,逐点计算累计3D距离(考虑海拔变化的真实徒步距离)
- 用
matplotlib绘制折线图,直观展示徒步过程中海拔随距离的变化趋势
内容的提问来源于stack exchange,提问作者Himalayan Hikers
相关产品推荐
相关产品推荐

