【问题标题】:How to retrieve a gridline axis tick positions?如何检索网格线轴刻度位置?
【发布时间】:2020-01-23 21:04:47
【问题描述】:

我正在尝试从 cartopy geoaxes 中检索 yaxis 和 xaxis 刻度位置。

据我了解,一个常见的matplotlib的Axes有内部方法:'axes.get_xticks'和'axes.get_yticks'。

尽管如此,来自 geoaxes 的 cartopy 网格线却没有。我怎样才能找回它们?

此外,当我尝试使用通用格式(即:“axes.get_yticks”)从地理坐标轴中检索刻度时,我会得到奇怪的坐标。

这是一个例子。

import pandas as pd
pd.set_option('display.width', 50000)
pd.set_option('display.max_rows', 50000)
pd.set_option('display.max_columns', 5000)


import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature


from matplotlib.offsetbox import AnchoredText

def main(projection = ccrs.Mercator(), drawlicense=True):

    fig = plt.figure(figsize=(9,7))
    ax = plt.axes(projection=projection)


    # Put a background image on for nice sea rendering.
    ax.stock_img()

    # Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
    states_provinces = cfeature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m',
        facecolor='none')

    SOURCE = 'Natural Earth'
    LICENSE = 'public domain'

    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(states_provinces, edgecolor='gray')

    # Add a text annotation for the license information to the
    # the bottom right corner.

    if drawlicense:

        text = AnchoredText(r'$\mathcircled{{c}}$ {}; license: {}'
                            ''.format(SOURCE, LICENSE),
                            loc='right',
                            bbox_transform=ax.transAxes,
                            bbox_to_anchor=(1.01, -0.02), 
                            prop={'size': 8}, 
                            frameon=False)

        ax.add_artist(text)

    plt.show()

    return ax

ax = main()

Gridliner = ax.gridlines(draw_labels=True)

在上述情况下,如果我尝试从地轴“ax”中检索 yticks,我最终会得到一组奇怪的值:


在:ax.get_yticks()

输出: 数组([-20000000., -15000000., -10000000., -5000000., 0., 5000000., 10000000., 15000000., 20000000.])


请注意,这些值不是以度数为单位的,尽管该图和选定的 cartopy 的投影都表示度数坐标。

因此,我做错了什么?如何获取地图各自的度坐标?

此致,

【问题讨论】:

  • 这些是轴坐标中的刻度位置,尝试ax.get_xticklabels() 获取标签(图中显示的内容)
  • 亲爱的威廉米勒,感谢您的回复。 “ax.get_xticklabels()”方法确实有效。然而,上述数字不能在轴坐标中。轴坐标在零和一之间变化,以上数值都在千以上。

标签: python-3.x matplotlib cartopy


【解决方案1】:

cartopy 坐标轴实际上并没有显示正常的 matplotlib 刻度。相反,您可以使用ax.gridlines 来获取一组显示网格的线条集合。返回的cartopy.mpl.gridliner.Gridliner 可用于查询行的位置。

请注意,投影不一定在 x 和 y 中可分离,因此网格线可能是曲线。

下面我们取这些线的第一点。

# create grid
gridliner = ax.gridlines(draw_labels=True)

# we need to draw the figure, such that the gridlines are populated
fig.canvas.draw()   

ysegs = gridliner.yline_artists[0].get_segments()
yticks = [yseg[0,1] for yseg in ysegs]

xsegs = gridliner.xline_artists[0].get_segments()
xticks = [xseg[0,0] for xseg in xsegs]

print(xticks)
print(yticks)

这会打印两个列表,其中包含第一个网格点的坐标:

[-180.0, -120.0, -60.0, 0.0, 60.0, 120.0]
[-80.0, -60.0, -40.0, -20.0, 0.0, 20.0, 40.0, 60.0, 80.0, 100.0]

【讨论】:

  • 像往常一样,我无意中充当了坎宁安定律实例的前半部分。一如既往的好答案
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2019-05-21
  • 1970-01-01
  • 2018-08-03
  • 2011-03-29
  • 2013-01-04
  • 2015-01-01
  • 1970-01-01
相关资源
最近更新 更多