Robbie Mallett
Robbie Mallett

Reputation: 183

Setting longitude of latitude tick labels in NorthPolarStereo Cartopy

I am making a plot of the Arctic region using code similar to:

from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import matplotlib.ticker as mticker
import cartopy.crs as ccrs

fig, ax = plt.subplots(1,1,figsize=(8,8),  subplot_kw={'projection': ccrs.NorthPolarStereo()})

ax.scatter([x[0] for x in max_coords[:-1]],
           [x[1] for x in max_coords[:-1]],
           color='k',
           s=50,
           marker='x',transform=ccrs.PlateCarree(),zorder=5,
           label='BSH Center\n1980-2020')

ax.set_extent([-2.633e+06, 2.696e+06, -6e+04, 2.9e+06], crs=ccrs.NorthPolarStereo()) 

ax.add_feature(cartopy.feature.LAND, edgecolor='black',zorder=1)

ax.legend(fontsize='x-large')

gl = ax.gridlines(draw_labels=True)

gl.xlocator = mticker.FixedLocator(np.concatenate([np.arange(-180,-89,15),np.arange(90,181,15)]))
gl.xformatter = LONGITUDE_FORMATTER
gl.xlabel_style = {'size': 12, 'color': 'k','rotation':0}

gl.yformatter = LATITUDE_FORMATTER
gl.ylocator = mticker.FixedLocator(np.arange(70,86,5),200)

enter image description here

As you can see, the ticks denoting the latitude are at a longitude of 135E. This means that the 75N tick is over an island and not legible. I would like these ticks to be on the line of 120E, but can't find out how to move them. I thought I could do it with the last two lines of my code above, but they don't seem to make any difference to the plot.

Upvotes: 0

Views: 2147

Answers (1)

swatchai
swatchai

Reputation: 18762

This needs some hacks on the labels. First, you can run this code:

gl.label_artists

and found that the in line labels are located at 138 degrees longitude. With this information, you can change it to other values, and I choose 126 with these lines of code:

plt.draw() 
for ea in gl.label_artists:
    if ea[1]==False:
        tx = ea[2]
        xy = tx.get_position()
        #print(xy)
        if xy[0]==138:
            #print("Yes")
            tx.set_position([126, xy[1]])

np-proj

Upvotes: 1

Related Questions