高纬度非矩形Cartopy投影下经纬度标签不显示问题求助
高纬度非矩形Cartopy地图显示经纬度标签问题
我基于Stack Overflow上的问题绘制了一幅高纬度非矩形Cartopy地图,希望在绘图的左侧/底部轴显示经纬度标签,但标签始终不显示。以下是用于生成该图的代码:
import numpy as np import cartopy import matplotlib.pyplot as plt import cartopy.crs as ccrs from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER import matplotlib.ticker as mticker import cartopy.feature as cf """ Plot Alaska """ # Map View Using Cartopy fig = plt.figure(figsize=(8,6)) xmin=-163 xmax=-120 ymin=50 ymax=71 proj = ccrs.LambertConformal(central_longitude=(xmin+xmax)/2, central_latitude=(ymin+ymax)/2) ax = fig.add_subplot(1, 1, 1, projection=proj) n = 20 aoi = mpath.Path( list(zip(np.linspace(xmin,xmax, n), np.full(n,ymax))) + \ list(zip(np.full(n,xmax), np.linspace(ymax,ymin, n))) + \ list(zip(np.linspace(xmax,xmin, n), np.full(n,ymin))) + \ list(zip(np.full(n,xmin), np.linspace(ymin,ymax, n))) ) ax.set_boundary(aoi, transform=ccrs.PlateCarree()) # Plot Ocean Borders ocean = cf.NaturalEarthFeature('physical','ocean',scale='50m',edgecolor='k',facecolor='lightblue',lw=1,linestyle='-') ax.add_feature(ocean) # Colored Land Background land = cf.NaturalEarthFeature('physical','land',scale='50m',facecolor='snow',lw=1,linestyle='--') ax.add_feature(land) ax.set_extent([xmin,xmax,ymin,ymax],crs=ccrs.PlateCarree()) ax.gridlines(draw_labels=True,crs=ccrs.PlateCarree(),x_inline=False,y_inline=False) gl.xlocator = mticker.FixedLocator([-160,-150,-140,-130,-120]) gl.ylocator = mticker.FixedLocator([50,55,60,65,70]) gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER plt.show()
生成的图无经纬度轴标签:
解决方法
标签不显示的核心原因是ax.gridlines()返回的Gridliner对象未正确赋值,且自定义边界后需要手动指定标签显示位置。修改步骤如下:
修复Gridliner变量赋值:
原代码中ax.gridlines()直接调用但未将结果赋值给gl,导致后续配置完全无效。需修改为:gl = ax.gridlines(draw_labels=True,crs=ccrs.PlateCarree(),x_inline=False,y_inline=False)明确指定标签显示位置:
自定义边界后Cartopy无法自动识别标签位置,需手动设置显示侧并调整样式:# 经度标签仅显示在底部 gl.top_labels = False gl.bottom_labels = True # 纬度标签仅显示在左侧 gl.left_labels = True gl.right_labels = False # 调整标签字体大小避免被裁剪 gl.xlabel_style = {'size': 10} gl.ylabel_style = {'size': 10}补充遗漏的导入:
原代码使用mpath.Path但未导入对应模块,需添加:import matplotlib.path as mpath
修改后的完整代码
import numpy as np import cartopy import matplotlib.pyplot as plt import cartopy.crs as ccrs from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER import matplotlib.ticker as mticker import cartopy.feature as cf import matplotlib.path as mpath # 补充遗漏的导入 """ Plot Alaska """ # Map View Using Cartopy fig = plt.figure(figsize=(8,6)) xmin=-163 xmax=-120 ymin=50 ymax=71 proj = ccrs.LambertConformal(central_longitude=(xmin+xmax)/2, central_latitude=(ymin+ymax)/2) ax = fig.add_subplot(1, 1, 1, projection=proj) n = 20 aoi = mpath.Path( list(zip(np.linspace(xmin,xmax, n), np.full(n,ymax))) + \ list(zip(np.full(n,xmax), np.linspace(ymax,ymin, n))) + \ list(zip(np.linspace(xmax,xmin, n), np.full(n,ymin))) + \ list(zip(np.full(n,xmin), np.linspace(ymin,ymax, n))) ) ax.set_boundary(aoi, transform=ccrs.PlateCarree()) # Plot Ocean Borders ocean = cf.NaturalEarthFeature('physical','ocean',scale='50m',edgecolor='k',facecolor='lightblue',lw=1,linestyle='-') ax.add_feature(ocean) # Colored Land Background land = cf.NaturalEarthFeature('physical','land',scale='50m',facecolor='snow',lw=1,linestyle='--') ax.add_feature(land) ax.set_extent([xmin,xmax,ymin,ymax],crs=ccrs.PlateCarree()) # 修复Gridliner赋值 gl = ax.gridlines(draw_labels=True,crs=ccrs.PlateCarree(),x_inline=False,y_inline=False) gl.xlocator = mticker.FixedLocator([-160,-150,-140,-130,-120]) gl.ylocator = mticker.FixedLocator([50,55,60,65,70]) gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # 指定标签显示位置和样式 gl.top_labels = False gl.bottom_labels = True gl.left_labels = True gl.right_labels = False gl.xlabel_style = {'size': 10} gl.ylabel_style = {'size': 10} plt.show()
内容的提问来源于stack exchange,提问作者kbiegseismic
相关产品推荐
相关产品推荐

