'How to remove N and E symbol in Cartopy lat-lon gridliner ticks
I am trying to remove capital N and E symbols in Cartopy gridline tick-labels. Just I want to keep the numeric value with degree symbol(°), e.g., 10°,15°,20°... instead of, 10°N,15°N,20°N..., as shown in below example map.
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'}
Solution 1:[1]
You should be able to do this by passing appropriate parameters to the gridline
method and using the appropriate formatters, like this:
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()
Solution 2:[2]
Gridliner
object generated by ax.gridlines(...)
provides all you need to access/manipulate the label texts.
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Stephane Raynaud |
Solution 2 | swatchai |