0 - Cartopy 正交投影中的 360 个经度标签

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

我正在尝试使用 Cartopy 生成一个正交图形。基本上,整个事情都按照我想要的方式工作......除了经度网格线上的标签。虽然经度惯例通常使用 0 - 180 E 或 0 - 180 W,但其他天体的惯例有时可能是 0 - 360 度。我想遵守后一个约定,以便与我其余工作中的经度约定保持一致。

我的问题是 LongitudeFormatter 似乎没有任何选项允许我简单地绘制带有 0 - 360 标签的经度网格线。该文档确实给出了如何使用 PlateCarree 完成此操作的示例(here),但我使用的是正交投影,并且尝试应用给定的示例只是告诉我“此格式化程序不能与非矩形投影一起使用。”话虽如此,LongitudeFormatter 似乎在正交投影方面完全可以正常工作?

这是一个演示我的问题的简化示例。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import (LongitudeFormatter, LatitudeFormatter,
                                LongitudeLocator,LatitudeLocator)

plotproj = ccrs.Orthographic(central_longitude=90, central_latitude=45)
mgeodetic = ccrs.Geodetic()

fig1,ax1 = plt.subplots(1,1,figsize=(10.0,10.0),constrained_layout=True,
                        subplot_kw={"projection":plotproj})
ax1.set_global()

#grid lines
longlocs = [0,30,60,90,120,150,180,210,240,270,300,330,360]
gl = ax1.gridlines(draw_labels=True,x_inline=True,
                    xpadding=0,ypadding=0,xlocs=longlocs)
gl.top_labels = False
gl.left_labels = False
gl.xlocator = LongitudeLocator(nbins=12)
gl.ylocator = LatitudeLocator(nbins=12)
gl.xformatter = LongitudeFormatter(auto_hide=False)
gl.yformatter = LatitudeFormatter(auto_hide=False)

ax1.invert_xaxis() #makes longitude run the opposite direction, necessary for my use case

此示例生成下图:

output of example code

我已经尝试了经度格式化程序可用的选项。

direction_label = False
只是使经度从 0 到 180 以及-180 到 0。
dateline_direction_label
zero_direction_label
仅影响0经线和180经线。
cardinal_labels
用于替换现有的 E 和 W 标签,但不影响值。

我觉得我一定忽略了一些明显的事情 - 如何绘制 0 到 360 经度标签?

python matplotlib cartopy
1个回答
0
投票
问:如何绘制 0 到 360 经度标签?
答:您可以更换符合您需要的新标签。

下面给出的代码应该可以解决问题。
只需将它们放在“ax1.invert_xaxis()”行的前面即可。
# (Relevant code)
# Generate the plot to enable access to the labels' attributes
plt.draw()

rep_vals = {'150°W': str(360-150),
            '120°W': str(360-120),
            '90°W': str(360-90),
            '60°W': str(360-60),
            '30°W': str(360-30),
            '0°': "0",
            '30°E': "30",
            '60°E': "60",
            '90°E': "90",
            '120°E': "120",
            '150°E': "150"
    }

# Iterate through the x-labels
for ea in gl.xlabel_artists:
    #print(ea, ea.get_position()[0], ea.get_visible())
    if ea.get_visible():
        # If visible, replace it with new label
        ea.set_text(rep_vals[ea.get_text()])
© www.soinside.com 2019 - 2024. All rights reserved.