我试图隐藏右侧的标签(又名纬度)在这个Cartopy map 使用right_labels = False,但只适用于某些值.但是,可以使用top_labels = Falsebottom_label=False隐藏顶部/底部标签.

复制:


 import numpy as np
    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    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.top_labels = False
    gl.right_labels = False

    plt.show()

这就是我所得到的:

funky axes Alaska

我怀疑这与使用ax.set_boundary()制作 map 有关,但在网上查看后,我找不到任何东西,除了这GitHub issue提到这个问题,但它是关闭的,所以我想他们修复了它.我正在使用Cartopy版本0.21.1.

这个问题是在研究另一个SO问题时产生的:Cartopy labels not appearing for high-latitude non-rectangular projection(所有代码积分到@kbiegseismic).

推荐答案

很明显,ax.set_boundary()混淆了.gridlines()内的过程,特别是当它在这种特定情况下需要隐藏一些y-标签时.因此,一些标签的可见性被错误设置.

我们需要等待下一个版本的Cartopy才能看到更好的gridlines()功能.如果您需要解决办法,可以try 下面的代码片段.只需将其放在现有代码的末尾.

# 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()

no-r-labels

如果将 map 边界修改为具有较小的额外缓冲区,如下所示:-

pg = 0.006
#xmin=-163 -pg
xmax=-120 +pg
ymin=50 -pg
#ymax=71 +pg

最终的地块将包括西经120°和北纬50°的缺失标签.

plot2

Python-3.x相关问答推荐

类型的可变性对变量的作用域有影响吗?

如何将CSV或FDF数据解析到Python词典并注入到模板PDF表单中?

S的两极是什么,相当于大Pandas 的`.ilo‘方法?

如何通过Pandas为不同的列集垂直设置列数据?

按长度和字母数字对Pandas 数据帧列进行排序

在 Python 中比较和排序列之间的值(带有不匹配列)

PyQt5 中耦合滑块和拨号小部件.解决结果不一致的问题

删除列表中的第二个出现

GEKKO 在没有不等式的模型中抛出不等式定义错误

将逗号分隔的字符串类型系列转换为整数列表 pandas

如果值超出上下限(异常值处理),则将值的数据框替换为 np.nan

Jupyter Notebook 拒绝打印一些字符串

在python中将字符串写入文本文件

matplotlib.pyplot 多边形,具有相同的纵横比和紧凑的布局

SqlAlchemy - 从 oracle db 中检索长文本

python - 错误 R10(启动超时)-> Web 进程未能在启动后 60 秒内绑定到 $PORT

如何将numpy数组图像转换为字节?

当默认 pip 为 pip2 时,升级 pip3 的正确格式是什么?

Windows 下 Python 3.x 的 OpenCV

如何删除目录? os.removedirs 和 os.rmdir 是否只用于删除空目录?