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

使用Cartopy绘制扇形地图问题:投影设置后仍生成矩形地图

解决Cartopy生成扇形地图的思路

要生成你需要的扇形地图,核心是让地图的视口形状变为扇形,而非仅通过set_extent限制经纬度范围(后者只会让内容显示在矩形框内,视口本身还是矩形)。以下是具体解决方法:

方法1:利用投影自带的cutoff参数(针对圆锥类投影)

你尝试过的LambertConformal本身支持cutoff参数,该参数会直接裁剪掉纬度低于设定值的区域,配合合适的投影参数可快速得到扇形效果:

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

# 启用LambertConformal并设置cutoff,裁剪30°纬度以下的区域
ProjPlot = ccrs.LambertConformal(central_longitude=-35, 
                                 central_latitude=70.0, 
                                 standard_parallels=(50,70),
                                 cutoff=30)

fig, ax = plt.subplots(subplot_kw={'projection': ProjPlot})

# 添加地图要素
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.gridlines(draw_labels=True)

# 设置经纬度范围,配合cutoff形成扇形
ax.set_extent([-90, 20, 35, 90], crs=ccrs.PlateCarree())

plt.show()

方法2:自定义扇形边界(适配所有圆锥投影)

如果使用AlbersEqualArea或EquidistantConic这类无cutoff参数的投影,可以通过ax.set_boundary结合matplotlib几何图形手动定义扇形边界:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.patches import Wedge

ProjPlot = ccrs.EquidistantConic(central_longitude=-35, 
                                 central_latitude=70.0, 
                                 standard_parallels=(50.0, 70.0))

fig, ax = plt.subplots(subplot_kw={'projection': ProjPlot})

# 添加地图要素
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.gridlines(draw_labels=True)

# 设置经纬度范围
ax.set_extent([-90, 20, 35, 90], crs=ccrs.PlateCarree())

# 定义扇形边界:以投影原点为中心,半径设为足够大数值,角度范围对应目标经度相对于中心的偏移
wedge = Wedge((0, 0), 1e6, 125, 235, transform=ProjPlot)
ax.set_boundary(wedge)

plt.show()

关键原理说明

  • set_extent仅限制显示的经纬度内容,但Cartopy默认用矩形视口包裹内容,所以最终呈现矩形。
  • cutoff是部分圆锥投影自带的裁剪功能,从投影层面直接过滤低纬度区域,天然形成扇形。
  • 自定义Wedge边界是手动修改视口形状,强制地图仅显示扇形区域内的内容,适配性更强。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 16:33:15