如何获取网格线轴刻度位置?

问题描述 投票:0回答:1

我正在尝试从一个Cartopy地轴检索yaxis和xaxis刻度位置。

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

但是,来自地轴的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()

出:array([-20000000。,-15000000。,-10000000。,-5000000。,0.,5000000.,10000000.,15000000.,20000000。]]


请注意,该值不是以度为单位,尽管该图形以及所选的Cartopy的投影状态都以度为坐标。

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

真诚地,

python-3.x matplotlib cartopy
1个回答
2
投票

胡萝卜轴实际上未显示正常的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]

0
投票

胡萝卜轴实际上未显示正常的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]
© www.soinside.com 2019 - 2024. All rights reserved.