Cartopy 的 matplotlib gridliner 接管了 xlabel 和 ylabel 并使用它来管理网格线和标签。https://github.com/SciTools/cartopy/blob/master/lib/cartopy/mpl/gridliner.py#L93
import matplotlib.pyplot as plt
import cartopy
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.COASTLINE)
gridlines = ax.gridlines(draw_labels=True)
# this would not function, due to the gridliner
# ax.set_xlabel('lon')
# ax.set_ylabel('lat')
plt.show()
如果您想向 cartopy 轴的轴实例添加标签,您应该放置它们,以免它们与网格线重叠。目前你需要手动完成此操作,例如:
import matplotlib.pyplot as plt
import cartopy
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.COASTLINE)
gridlines = ax.gridlines(draw_labels=True)
ax.text(-0.07, 0.55, 'latitude', va='bottom', ha='center',
rotation='vertical', rotation_mode='anchor',
transform=ax.transAxes)
ax.text(0.5, -0.2, 'longitude', va='bottom', ha='center',
rotation='horizontal', rotation_mode='anchor',
transform=ax.transAxes)
plt.show()
您需要调整 ax.text 放置的值以获得每种情况下您想要的效果,这可能有点令人沮丧,但它很实用。
添加到 cartopy 来自动化此放置将是一个很好的功能。