Skip to main content
 首页 » 编程设计

python之Cartopy 极地立体图中轮廓的奇怪行为

2025年02月15日8dflying

我正在尝试使用 Cartopy 在北极立体 map 投影上创建等高线图。我使用 add_cycular_point() 尝试解决经度 0 和经度 35X 之间存在间隙的问题,并按照文档 (always_circular_stereographic) 中的示例设置 map 轴。

当我调用 plt.contour 时,我得到以下图。看起来等高线绘图仪在从 355 到 0 经度的过渡处变得困惑,并在全局范围内发送等高线。

这是我的代码:

import numpy as np 
import cartopy.crs as ccrs 
from cartopy.util import add_cyclic_point 
import matplotlib.pyplot as plt 
 
def define_map(): 
    from matplotlib.path import Path 
 
    fig = plt.figure(figsize=(10,10)) 
    ax = plt.axes(projection=ccrs.NorthPolarStereo()) 
    ax.coastlines() 
 
    # From example: http://scitools.org.uk/cartopy/docs/latest/examples/always_circular_stereo.html 
    theta = np.linspace(0, 2*np.pi, 100) 
    center, radius = [0.5, 0.5], 0.5 
    verts = np.vstack([np.sin(theta), np.cos(theta)]).T 
    circle = Path(verts * radius + center) 
 
    ax.set_boundary(circle, transform=ax.transAxes) 
    return(fig, ax) 
lats = np.arange(65,91,5) 
lons = add_cyclic_point(np.arange(0,359,5)) 
data = add_cyclic_point(np.random.random((len(lats),len(lons)-1))) 
 
 
fig, ax = define_map() 
plt.contour(lons,lats,data,5,transform=ccrs.PlateCarree(), cmap=plt.cm.Blues) 
plt.colorbar(fraction=0.05, shrink=0.9) 
plt.show() 

如何正确绘制 Cartopy 等高线图? 另外,为什么等高线只出现在 transform=ccrs.PlateCarree() 而不是 transform=ccrs.NorthPolarStereo()?

请您参考如下方法:

显然 add_cyclic_point 函数只是为了数据;轮廓例程对待 0 不同于 360。所以简单的解决方法是设置

lons = np.arange(0,360,5)