我正在尝试使用 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
此示例生成下图:
我已经尝试了 LongitudeFormatter 的可用选项。
direction_label = False
只是使经度从 0 到 180 以及 -180 到 0。
dateline_direction_label
和 zero_direction_label
仅影响 0 和 180 经线。
cardinal_labels
用于替换现有的 E 和 W 标签,但不影响值。
我觉得我一定忽略了一些明显的事情 - 如何绘制 0 到 360 经度标签?
最佳答案
Q: How do I plot 0 to 360 longitude labels?
A: You can replace the labels with new ones that meet your needs.
下面给出的代码应该可以解决问题。
将其放在 ax1.invert_xaxis()
行之前。
fig1, ax1 = plt.subplots(1, 1, figsize=(10.0, 10.0), constrained_layout=True, subplot_kw={"projection": plotproj})
ax1.set_global()
#grid lines
gl = ax1.gridlines(draw_labels=True, x_inline=True, xpadding=0, ypadding=0)
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)
# (Relevant code)
# Generate the plot to enable access to the labels' attributes
ax1.draw(fig1.canvas.get_renderer())
# The current labels and the new ones to use
# If you need degree symbol just add + "°"
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()])
ax1.invert_xaxis()
关于python - Cartopy 正交投影中的 0 - 360 经度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76996496/