python-3.x - 在 Cartopy 中隐藏高纬度非矩形投影的右侧轴(纬度)标签

标签 python-3.x matplotlib axis-labels cartopy

我试图使用 right_labels = False 隐藏此 Cartopy map 中的右侧标签(又名纬度),但仅适用于某些值。但是,可以使用 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() 功能。如果您需要解决方法,可以尝试下面的代码片段。只需将其放在现有代码的末尾即可。

# 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 - 在 Cartopy 中隐藏高纬度非矩形投影的右侧轴(纬度)标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75597673/

相关文章:

python-3.x - 为什么不能将多个客户端同时连接到服务器? Python

python - matplotlib图例中符号左侧的文字

jquery - 如何向 highcharts 中的 x 轴标签添加工具提示?

d3.js - xAxis 刻度的 D3 组标签

python - 如何将unicode日文从一个文件写入另一个文件?

python-3.x - 如何将最高的 matplotlib 散点图 c 值发送到前面?

python - 如何在 python 中遍历列表时删除 None

python - 在 matplotlib 中将 x 轴移动到绘图的顶部

python - matplotlib 中的颜色

r - R 的 NODF 图中没有显示轴标签