I'm moving from Basemap to Cartopy and want to plot data for the Arctic Ocean that covers the pole.
I've decided to use the NorthPolarStereo() projection and am happy to use either pcolormesh or contourf. Unfortunately my field of data doesn't show up when I execute the following code:
import cartopy.crs as ccrso
from netCDF4 import Dataset
def import_envisat_field(year,
month):
data_dir = f'/media/robbie/Seagate Portable Drive/Envisat_thickness/{year}/'
file = f'ESACCI-SEAICE-L3C-SITHICK-RA2_ENVISAT-NH25KMEASE2-{year}{month}-fv2.0.nc'
data = Dataset(data_dir+file)
return(data)
# Import data
data = import_envisat_field("2003","02")
# Make plot
fig = plt.figure(figsize=[10, 5])
ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax.add_feature(cartopy.feature.OCEAN, zorder=0)
ax.add_feature(cartopy.feature.LAND, zorder=1, edgecolor='black')
extent = 2500000
ax.set_extent((-extent,
extent,
-extent,
extent),
crs=ccrs.NorthPolarStereo())
ax.gridlines()
lon = np.array(data['lon'])
lat = np.array(data['lat'])
field = np.array(data['sea_ice_thickness'])[0]
print(lon.shape,lat.shape,field.shape)
# This print command gives (432, 432) (432, 432) (432, 432)
plt.pcolormesh(lon, lat, field,zorder=2,
transform=ccrs.NorthPolarStereo())
plt.show()
The data plots in a straightforward way using Basemap, but executing the above code just gives me a nice picture of the Arctic ocean but without my data on it.
I've also tried replacing plt.pcolormesh with ax.pcolormesh but that didn't work either.
Cartopy output:
Basemap output with the same data:
If your data coordinates are latitude and longitude you need to use the PlateCarree transform:
plt.pcolormesh(lon, lat, field,zorder=2, transform=ccrs.PlateCarree())
The transform describes the data coordinates and is independent from the projection you'd like to plot on. See this guide in the Cartopy documentation for more details https://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html