【问题标题】:How to Add More Lines of Longitude/latitude in NorthPolarStereo Projection如何在北极立体投影中添加更多经度/纬度线
【发布时间】:2026-01-06 13:55:02
【问题描述】:

我正在尝试增加 Python 中 NorthPolarStereo 投影上经线出现的间隔。我尝试增加/定义刻度/lim(对于 x 和 y 坐标)并返回错误“具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()” .我也尝试过改变范围的界限。我发现一篇文章提到了使用底图,但我认为我不需要整体使用它。我用于投影的基本代码的链接是here。 代码片段在这里:

# Generate axes, using Cartopy, drawing coastlines, and adding features
fig=plt.figure(figsize=(10,10))

projection = ccrs.NorthPolarStereo()
ax = plt.axes(projection=projection)
ax.coastlines(linewidths=0.5)


ax.add_feature(cfeature.LAND, facecolor='lightgray')
ax.set_extent([-180,180,90,0],ccrs.PlateCarree())
gl = ax.gridlines(crs=ccrs.PlateCarree(),linestyle="--", linewidth=1, color='k', alpha=0.5)


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 = mpath.Path(verts * radius + center)

ax.set_xticks([180,150,120,90,60,30])
ax.set_boundary(circle, transform=ax.transAxes)
p=wrap_U.plot.contour(ax=ax,vmin=-8,vmax=16, transform=ccrs.PlateCarree(),
                        levels = 7, linewidths=0.5, cmap='k')

ax.clabel(p, np.arange(-8,16,8),fmt='%d', inline=1, fontsize=14)


gvutil.set_titles_and_labels(ax, 
                                 lefttitle="Zonal Wind", righttitle="m/s")

  #This line leads to truth value error  
gvutil.set_axes_limits_and_ticks(ax, xticks=np.linspace(-180, 180), xticklabels=['180S', '90S', '0', '90N', '180N'],
                                        ylim=ax.get_ylim()[::-1], yticks=U)



# Show the plot
plt.show()

我确实设置了一个自定义 anaconda 环境,所以如果需要更多信息,我也可以链接到哪里可以找到该环境。

【问题讨论】:

  • 你检查我的答案了吗?尝试运行我的代码并给我反馈。

标签: python dictionary projection cartopy


【解决方案1】:

您似乎正在尝试设置一组不匹配的 xticks 和 xticklabels

gvutil.set_axes_limits_and_ticks(ax, xticks=np.linspace(-180, 180), xticklabels=['180S', '90S', '0', '90N', '180N'],
                                        ylim=ax.get_ylim()[::-1], yticks=U)

您的代码生成 50 个 xticks np.linspace(-180,180)numpy linspace 默认值)

但只有 5 个标签 xticklabels=['180S', '90S', '0', '90N', '180N']

作为替代方案,我建议尝试 xticks=np.linspace(-180,180,5) 或暂时删除特定的 xticklabels

【讨论】:

    【解决方案2】:

    要根据您的规范绘制纬度平行线和子午线弧线,您必须在ax.gridlines() 中指定xlocsylocs 的正确值。对于它们的标签,您可以使用ax.text()。尝试运行下面的代码并根据需要进行修改。

    import matplotlib.pyplot as plt
    import numpy as np
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import matplotlib.path as mpath
    
    fig = plt.figure(figsize=[8,8])
    projection = ccrs.NorthPolarStereo()
    ax = plt.axes(projection=projection)
    ax.coastlines(linewidths=0.5)
    
    ax.add_feature(cfeature.LAND, facecolor='lightgray')
    ax.set_extent([-180,180,90,0], ccrs.PlateCarree())
    
    # specifying xlocs/ylocs yields number of meridian/parallel lines
    dmeridian = 30  # spacing for lines of meridian
    dparallel = 15  # spacing for lines of parallel 
    num_merid = 360/dmeridian + 1
    num_parra = 90/dparallel + 1
    gl = ax.gridlines(crs=ccrs.PlateCarree(), \
                      xlocs=np.linspace(0, 360, num_merid), \
                      ylocs=np.linspace(0, 90, num_parra), \
                      linestyle="--", linewidth=1, color='k', alpha=0.5)
    
    theta = np.linspace(0, 2*np.pi, 120)
    verts = np.vstack([np.sin(theta), np.cos(theta)]).T
    center, radius = [0.5, 0.5], 0.5
    circle = mpath.Path(verts * radius + center)
    
    ax.set_boundary(circle, transform=ax.transAxes)  #without this; get rect bound
    
    # for label alignment
    va = 'center' # also bottom, top
    ha = 'center' # right, left
    degree_symbol=u'\u00B0'
    
    # for locations of (meridional/longitude) labels
    lond = np.linspace(0, 360, num_merid)
    latd = np.zeros(len(lond))
    
    for (alon, alat) in zip(lond, latd):
        projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
        if alon>0 and alon<180:
            ha = 'left'
            va = 'center'
        if alon>180 and alon<360:
            ha = 'right'
            va = 'center'
        if np.abs(alon-180)<0.01:
            ha = 'center'
            va = 'bottom'
        if alon==0.:
            ha = 'center'
            va = 'top'
        if (alon<360.):
            txt =  ' {0} '.format(str(int(alon)))+degree_symbol
            ax.text(projx1, projy1, txt, \
                    va=va, ha=ha, color='g')
    
    # for locations of (meridional/longitude) labels
    # select longitude: 315 for label positioning
    lond2 = 315*np.ones(len(lond))
    latd2 = np.linspace(0, 90, num_parra)
    va, ha = 'center', 'center'
    for (alon, alat) in zip(lond2, latd2):
        projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
        txt =  ' {0} '.format(str(int(alat)))+degree_symbol
        ax.text(projx1, projy1, \
                    txt, va=va, ha=ha, \
                    color='r') 
    
    # Show the plot
    plt.show()
    

    输出图:

    【讨论】:

      最近更新 更多