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 以自动执行此放置将是一个不错的功能。
