0

The code below produces a map of the North Atlantic Ocean. However, the latitude labels are misplaced (want them outside, not inside) and not on both sides. I think this has to do with how I set the extent, but I do not know any other way to get this conic shape I want.

I would appreciate any answer to one or both of these questions:

  1. Is there a better way to set the extent to get that conic shape?
  2. How can I have my y-labels on the left and outside the figure?

enter image description here

# Import modules
import cartopy
import cartopy.crs as ccrs
import matplotlib.path as mpath
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

# Define projections
dataproj = ccrs.PlateCarree()
mapproj  = ccrs.Stereographic(central_longitude=-30)

# Plot figure
fig = plt.figure()
ax = plt.axes(projection=mapproj)

# Add some features
ax.add_feature(cartopy.feature.LAND, edgecolor='black')
ax.add_feature(cartopy.feature.OCEAN)

###
# I want to have kind of a conic shape. I manage to do this by adapting some code from
# https://stackoverflow.com/a/65690841/9970523
#
# Lon and Lat Boundaries
xlim = [-60, 0]
ylim = [50, 65]
lower_space = 3 
rect = mpath.Path([[xlim[0], ylim[0]],
                   [xlim[1], ylim[0]],
                   [xlim[1], ylim[1]],
                   [xlim[0], ylim[1]],
                   [xlim[0], ylim[0]],
                   ]).interpolated(20)
proj_to_data   = ccrs.PlateCarree()._as_mpl_transform(ax) - ax.transData
rect_in_target = proj_to_data.transform_path(rect)
ax.set_boundary(rect_in_target)
ax.set_extent([xlim[0], xlim[1], ylim[0] - lower_space, ylim[1]])
#
# End of https://stackoverflow.com/a/65690841/9970523
###
# lats/lons labels and ticks
gl = ax.gridlines(crs=ccrs.PlateCarree())
gl.xlocator = mticker.FixedLocator([-60, -45, -30,-15, 0])
gl.ylocator = mticker.FixedLocator([50, 55, 60, 65])
gl.bottom_labels = True
gl.left_labels   = True
gl.top_labels    = True
gl.right_labels  = True
Anne Sophie
  • 117
  • 2
  • 12
  • The y-labels seem to be where the longitude is zero. If I change `gl=ax.gridlines(crs=ccrs.PlateCarree())` by `gl=ax.gridlines(crs=ccrs.PlateCarree(central_longitude=30))` I will now have the latitude in the middle of my plot, but also the longitudes would be all shifted... Not so helpful... – Anne Sophie Mar 09 '23 at 22:06

1 Answers1

2

I finally got the answer from https://stackoverflow.com/a/75587005/9970523

It is important to have draw_labels=True, x_inline=False, y_inline=False in ax.gridlines()

See the updated code below.

enter image description here

# Import modules
import cartopy
import cartopy.crs as ccrs
import matplotlib.path as mpath
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker

# Define projections
dataproj = ccrs.PlateCarree()
mapproj  = ccrs.Stereographic(central_longitude=-30)

# Plot figure
fig = plt.figure()
ax = plt.axes(projection=mapproj)

# Add some features
ax.add_feature(cartopy.feature.LAND, edgecolor='black')
ax.add_feature(cartopy.feature.OCEAN)

###
# I want to have kind of a conic shape. I manage to do this by adapting some code from
# https://stackoverflow.com/a/65690841/9970523
#
# Lon and Lat Boundaries
xlim = [-60, 0]
ylim = [50, 65]
lower_space = 3 
rect = mpath.Path([[xlim[0], ylim[0]],
                   [xlim[1], ylim[0]],
                   [xlim[1], ylim[1]],
                   [xlim[0], ylim[1]],
                   [xlim[0], ylim[0]],
                   ]).interpolated(20)
proj_to_data   = ccrs.PlateCarree()._as_mpl_transform(ax) - ax.transData
rect_in_target = proj_to_data.transform_path(rect)
ax.set_boundary(rect_in_target)
ax.set_extent([xlim[0], xlim[1], ylim[0] - lower_space, ylim[1]])
#
# End of https://stackoverflow.com/a/65690841/9970523
###
# lats/lons labels and ticks
gl = ax.gridlines(draw_labels=True,x_inline=False,y_inline=False, crs=ccrs.PlateCarree())
gl.xlocator = mticker.FixedLocator([-45, -30,-15])
gl.ylocator = mticker.FixedLocator([55, 60])
gl.bottom_labels = True
gl.left_labels   = True
gl.top_labels    = True
gl.right_labels  = True
Anne Sophie
  • 117
  • 2
  • 12