如何删除 Cartopy lat-lon gridliner 刻度中的 N 和 E 符号

How to remove N and E symbol in Cartopy lat-lon gridliner ticks

我正在尝试删除 Cartopy 网格线刻度标签中的大写 NE 符号。只是我想保留带有度数符号(°)的数值,例如10°,15°,20°...而不是10°N,15°N,20°N ...,如下所示示例地图。

Data.plot.pcolormesh(ax=ax ,cmap=plt.cm.get_cmap('seismic'),
                      add_colorbar=False,add_labels=False,
                      transform=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle='-')
ax.add_feature(cartopy.feature.LAND, zorder=100, edgecolor='k')
gl = ax.gridlines(draw_labels=True, linestyle='--')
gl.yformatter=LATITUDE_FORMATTER
gl.ylabels_right=False
gl.ylabels_left=False
gl.xlabels_bottom=True
gl.xlabels_top=False
gl.ylabel_style={'size':10,'weight':'bold'}

任何破解这个的猜测! 谢谢

Gridlinerax.gridlines(...) 生成的对象提供了 access/manipulate 标签文本所需的一切。

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import cartopy
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

plt.figure(figsize=[10, 8.5])
proj = ccrs.PlateCarree()
ax = plt.axes(projection=proj)

ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle='-')
ax.add_feature(cartopy.feature.LAND, zorder=100, edgecolor='k')
gl = ax.gridlines(draw_labels=True, linestyle='--')
gl.yformatter=LATITUDE_FORMATTER

# Use the more recent methods
#gl.ylabels_right=False
gl.right_labels =False
#gl.ylabels_left=False
gl.left_labels =True
#gl.xlabels_bottom=True
gl.bottom_labels =True
#gl.xlabels_top=False
gl.top_labels =False

gl.ylabel_style={'size':10,'weight':'bold'}

# Generate/draw the plot so that `gl`'s properties are created
#  and the subsequent lines of code are possible 
plt.draw()

# Manipulate 'gl' by accessing all the texts
#  and chnage their contents as required
for ea in gl._labels:
    oldtxt = ea[2].get_text()
    #print("Original:", ea[2].get_text())
    newtxt = oldtxt.replace("W","")
    newtxt = newtxt.replace("N","")
    newtxt = newtxt.replace("E","")
    newtxt = newtxt.replace("S","")
    #print("New", newtxt)
    ea[2].set_text(newtxt)

plt.show()

您应该能够通过将适当的参数传递给 gridline 方法并使用适当的格式化程序来执行此操作,如下所示:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter

plt.figure(figsize=[10, 8.5])
proj = ccrs.PlateCarree()
ax = plt.axes(projection=proj)

ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle="-")
ax.add_feature(cartopy.feature.LAND, zorder=100, edgecolor="k")

cardinal_labels = {"east": "", "west": "", "north": "", "south": ""}
latitude_formatter = LatitudeFormatter(cardinal_labels=cardinal_labels)
longitude_formatter = LongitudeFormatter(cardinal_labels=cardinal_labels)
gl = ax.gridlines(
    draw_labels=["left", "bottom"],
    linestyle="--",
    xformatter=longitude_formatter,
    yformatter=latitude_formatter,
    ylabel_style={"size": 10, "weight": "bold"},
)
plt.show()