python 对于高纬度非矩形投影,不显示Cartopy标注

inkz8wg9  于 2023-02-28  发布在  Python
关注(0)|答案(1)|浏览(180)

我根据这个stackoverflow question绘制了一个高纬度的非矩形Map。
由于某种原因,我没有得到任何x和y经度/纬度标签,即使我希望标签位于图的左/下轴。
下面是我用来生成这个图的代码:

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()

这是你得到的数字:
enter image description here
如何使x轴和y轴标签显示在非矩形图上?

9nvpjoqh

9nvpjoqh1#

您只需要设置gl = ax.gridlines(draw_labels=True,crs=ccrs.PlateCarree(),x_inline=False,y_inline=False)

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 matplotlib.path as mpath
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())
# Set gridlines to variable so you can manipulate them
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

plt.show()

相关问题