I am trying to plot some climate data on a map of a part of Scandinavia. In the codes below I left out the non-relevant parts, focusing on the parts relying on the transform and projection.
Using PlateCarree() as both transform and projection keywords works, but in that projection the map is skewed:
dataset_LU = Dataset('data.nc', 'r')
LU = dataset_LU.variables['LU_INDEX'][0,:,:]
lats = dataset_LU.variables['XLAT_M'][0,:,:]
lons = dataset_LU.variables['XLONG_M'][0,:,:]
projection = crs.PlateCarree()
transform = crs.PlateCarree()
fig, axs = plt.subplots(nrows = nrows, ncols = ncolumns,
subplot_kw = {'projection': projection},
figsize = (11,8.5))
cs = axs.contourf(lons, lats, LU, 60, transform=transform,
levels=levels, cmap=cmap2, extend='both')
axs.set_extent(bbox, projection)
When using Mercator() as both transform and projection, the map shows but without the black lines, with wrong ocean colour and still on the skewed projection:
dataset_LU = Dataset('data.nc', 'r')
LU = dataset_LU.variables['LU_INDEX'][0,:,:]
lats = dataset_LU.variables['XLAT_M'][0,:,:]
lons = dataset_LU.variables['XLONG_M'][0,:,:]
projection = crs.Mercator()
transform = crs.Mercator()
fig, axs = plt.subplots(nrows = nrows, ncols = ncolumns,
subplot_kw = {'projection': projection},
figsize = (11,8.5))
cs = axs.contourf(lons, lats, LU, 60, transform=transform,
levels=levels, cmap=cmap2, extend='both')
axs.set_extent(bbox, projection)
When using Mercator() as projection and PlateCarree() as transform the code takes a long time and then shows an empty plot:
dataset_LU = Dataset('data.nc', 'r')
LU = dataset_LU.variables['LU_INDEX'][0,:,:]
lats = dataset_LU.variables['XLAT_M'][0,:,:]
lons = dataset_LU.variables['XLONG_M'][0,:,:]
projection = crs.Mercator()
transform = crs.PlateCarree()
fig, axs = plt.subplots(nrows = nrows, ncols = ncolumns,
subplot_kw = {'projection': projection},
figsize = (11,8.5))
cs = axs.contourf(lons, lats, LU, 60, transform=transform,
levels=levels, cmap=cmap2, extend='both')
axs.set_extent(bbox, projection)
What am I misunderstanding here? How can I solve the issue and display my data on the Mercator (or any other non-PlateCarree) projection?
Turns out the fix was really easy. The only problem with the last plot was that projection was also specified in axs.set_extent(bbox, projection). This messed up the plotting and resulted in a white field. Changing this to axs.set_extent(bbox) fixed the issue!
Transforming the data to fit the projection is the reason it takes so long to load.