高纬度非矩形投影未显示 Cartopy 标签

Cartopy labels not appearing for high-latitude non-rectangular projection

提问人:kbiegseismic 提问时间:2/28/2023 最后编辑:just_another_profilekbiegseismic 更新时间:3/2/2023 访问量:362

问:

我根据这个堆栈溢出问题绘制了一张高纬度非矩形卡托姆图。

出于某种原因,即使我想要在绘图的左/下轴上添加标签,我也没有得到任何 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()

这是你得到的数字:

fig no axes

如何让我的 x 轴和 y 轴标签显示在非矩形图上?

python matplotlib axis-labels cartopy 网格线

评论


答:

2赞 just_another_profile 2/28/2023 #1

你只需要设置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()

alaska with labels


编辑:

多亏了@swatchai,以下代码片段处理了右侧的 y 标签:

# Generate the plot to enable access to the labels' attributes
plt.draw()

# Iterate for the y-labels
# The right labels have x coordinates > 0
# The left labels < 0
for ea in gl.ylabel_artists:
    right_label = ea.get_position()[0] > 0
    # print(ea, ea.get_position()[0], ea.get_visible())
    if right_label:
        ea.set_visible(False)

plt.show()

查看@swatchai的完整答案了解更多详情!

评论

0赞 kbiegseismic 2/28/2023
是的。。。谢谢你,因为这是我最初拥有的,但我整天都在恐慌调试,并出于某种原因把它拿出来。这并没有改变我的数字。
1赞 just_another_profile 2/28/2023
添加 gl = ax.gridline() 它对你不起作用吗?你用的是哪个 Cartopy 版本?我正在使用 0.21.1
0赞 kbiegseismic 2/28/2023
你知道吗。。。可能就是这样。看来我有一个旧版本 0.18.0。我怀疑更新我的环境需要一点时间。
2赞 kbiegseismic 2/28/2023
哦,哎呀,是的!谢谢。升级软件包肯定有效。
1赞 swatchai 3/1/2023
请检查我对您问题的回答,并尝试应用此处的解决方法以获得所需的图。我会投票支持你的答案。