You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cartopy设置central_longitude后飞行轨迹绘制位置异常求助

解决方法

问题核心是绘制散点时未指定数据的坐标参考系(CRS)。当你修改轴的投影(设置central_longitude)后,轴的显示坐标系发生了偏移,但你的经纬度数据属于地理坐标系(PlateCarree,即WGS84),必须明确告知matplotlib数据的坐标系,才能让Cartopy自动将数据转换到轴的投影坐标系中,实现轨迹与地图匹配。

关键修改

在scatter调用中添加crs=ccrs.PlateCarree()参数,同时修正代码中的笔误:

# 替换原散点绘制代码
ax.scatter(lon, lat, s=20, crs=ccrs.PlateCarree())

修正后的完整代码

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature

fig = plt.figure(figsize=(16,8))
ax = plt.subplot(1,1,1, projection=ccrs.PlateCarree(central_longitude=40))

ax.coastlines()
# 添加网格线并自定义
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2)
gl.xlabels_top = False
gl.ylabels_left = False
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND)
ax.set_ylim(-90, 60)

# 绘制经纬度散点,指定数据的CRS
ax.scatter(lon, lat, s=20, crs=ccrs.PlateCarree())

plt.show()

额外说明

代码中的几处笔误需要注意:

  • fig size(16,8)需改为figsize=(16,8)
  • plot.subplot通常应为plt.subplot(除非你将matplotlib.pyplot别名为plot)
  • im.ax.scatter中的im是多余的,直接使用ax.scatter即可

内容的提问来源于stack exchange,提问作者Climate Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 15:24:20