10

使用 cartopy 地图时,我无法添加 xlabel 或 ylabel。有没有办法做到这一点?我不是在寻找刻度标签。

import matplotlib.pyplot as plt
import cartopy
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.COASTLINE)
ax.set_xlabel('lon')
ax.set_ylabel('lat')
plt.show()
4

2 回答 2

17

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

标记网格线

于 2016-02-18T14:07:10.307 回答
6

偶然发现跑步...

import matplotlib.pyplot as plt
import cartopy
ax = plt.axes(projection=cartopy.crs.PlateCarree())
ax.add_feature(cartopy.feature.COASTLINE)
ax.set_xlabel('lon')
ax.set_ylabel('lat')

ax.set_xticks([-180,-120,-60,0,60,120,180])
ax.set_yticks([-90,-60,-30,0,30,60,90])

plt.show()

...它打印 xticks 和 yticks 以及 xlabel 和 ylabel。在 xticks 和 yticks 已经定义的其他情况下,它们将被恢复...

ax.set_xticks(ax.get_xticks())
ax.set_yticks(ax.get_yticks())

或者如果它们是在地图限制之外自动生成的

ax.set_xticks(ax.get_xticks()[abs(ax.get_xticks())<=180])
ax.set_yticks(ax.get_yticks()[abs(ax.get_yticks())<=90])

在此处输入图像描述

对于添加网格...

plt.grid()

在此处输入图像描述

于 2019-09-09T15:21:51.803 回答