python - cartopy set_xlabel set_ylabel(不是刻度标签)

标签 python matplotlib axis-labels cartopy

使用 cartopy map 时,我无法添加 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()

最佳答案

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

labelled gridliner

关于python - cartopy set_xlabel set_ylabel(不是刻度标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35479508/

相关文章:

python - Django REST框架: AttributeError: 'DeferredAttribute' object has no attribute 'isoformat'

python - CPU 使用率(top 的输出)如何总计超过 100%?

python - pyplot colorbar 有一个不需要的额外小数。最高值应为 1.00,但显示为 1.05

python - matplotlib 中的阿拉伯语文本

matlab - 仅具有 ylabel 的子图

matlab - 如何在 Matlab 绘图中插入两个 X 轴

python - 在 Python 中锁定文件

python - 我不知道如何使用 "#' s"和 "*' s"在 python 中制作这个形状

python - 如何将数据值转换为matplotlib的颜色信息?

r - 增加 y 轴刻度标签 ggplot2 上的间距