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

如何在Cartopy中通过指定坐标旋转视图对齐X轴与海岸线

旋转亚马逊区域地图使X轴与海岸线对齐

要实现将X轴与海岸线对齐的效果,我们需要使用Cartopy的旋转极投影(RotatedPole),通过计算海岸线的走向角度来定义旋转后的坐标系。以下是具体步骤和修改后的代码:

步骤说明

  1. 计算旋转角度:选取海岸线上两个点,计算其相对于正东方向的夹角,以此确定需要旋转的角度。
  2. 创建旋转投影:使用ccrs.RotatedPole定义旋转后的坐标系,使海岸线走向与新的X轴平行。
  3. 转换坐标范围:将原始经纬度范围转换为旋转坐标系下的范围,确保地图显示区域正确。
  4. 添加地图要素:加载海岸线、边界等地理要素时,需指定其原始坐标系为PlateCarree,确保正确转换到旋转投影中。

修改后的代码

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

# 1. 计算海岸线走向的旋转角度
# 选取海岸线上的两个参考点(经纬度)
point_nw = (-70, 5)   # 西北端参考点
point_se = (-45, -5)  # 东南端参考点

delta_lon = point_se[0] - point_nw[0]
delta_lat = point_se[1] - point_nw[1]

# 计算海岸线相对于正东方向的夹角(单位:度)
angle_deg = np.degrees(np.arctan2(delta_lat, delta_lon))

# 2. 创建旋转极投影:将坐标系逆时针旋转 -angle_deg 度,使海岸线与X轴对齐
rotated_crs = ccrs.RotatedPole(pole_longitude=90 + angle_deg, pole_latitude=90)

# 3. 创建绘图对象并设置旋转投影
fig, ax = plt.subplots(figsize=(10, 6), subplot_kw={'projection': rotated_crs})

# 原始地理范围(经纬度)
geo_extent = [-74, -32, -5, 15]
# 将地理范围转换为旋转坐标系下的坐标
x_min, y_min = rotated_crs.transform_point(geo_extent[0], geo_extent[2], ccrs.PlateCarree())
x_max, y_max = rotated_crs.transform_point(geo_extent[1], geo_extent[3], ccrs.PlateCarree())
ax.set_extent([x_min, x_max, y_min, y_max], crs=rotated_crs)

# 4. 添加地图要素(指定原始坐标系为PlateCarree)
ax.coastlines(resolution='10m', transform=ccrs.PlateCarree())
ax.add_feature(cfeature.BORDERS, linestyle=':', transform=ccrs.PlateCarree())

# 添加旋转坐标系下的网格线与标签
gl = ax.gridlines(draw_labels=True, linestyle='--', color='gray')
gl.top_labels = False
gl.right_labels = False
gl.xlabel_style = {'size': 10, 'color': 'black'}
gl.ylabel_style = {'size': 10, 'color': 'black'}

plt.show()

关键细节

  • 旋转角度调整:如果需要对齐的海岸线段不同,可以修改point_nw和point_se的坐标,重新计算旋转角度。
  • 投影参数:RotatedPole的pole_longitude和pole_latitude定义了旋转后极点的地理坐标,这里通过计算确保海岸线与X轴平行。
  • 坐标转换:所有地理要素(如海岸线)都需要通过transform=ccrs.PlateCarree()指定原始坐标系,才能正确渲染到旋转投影中。

内容的提问来源于stack exchange,提问作者iury simoes-sousa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 03:17:03