I am trying to plot some climate data on a map of a part of Scandinavia. 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,:,:]
bbox = [2.5,20,57, 71.5]
projection = crs.PlateCarree()
transform = crs.PlateCarree()
nrows = 1
ncolumns = 1
vmin = 0
vmax = 24
levels = np.linspace(vmin, vmax, 24+1)
fig, axs = plt.subplots(nrows = nrows, ncols = ncolumns,
subplot_kw = {'projection': projection},
figsize = (11,8.5))
cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list("my_colormap", ('#00ffdd', '#acb20c', '#e8a202', '#ffff00', '#ff0000',
'#ff7b59', '#ffb29f', '#bf0041', '#e16173', '#784b04',
'#0e6b1d', '#00b737', '#09ff00', '#81d41a', '#afd095',
'#16a69a', '#55215b', '#6c2dcc', '#8e86ae', '#000000',
'#333333', '#666666', '#999999', '#eeeeee'),
N=24, gamma=1.0)
cs = axs.contourf(lons, lats, LU, 60, transform=transform, levels=levels, cmap=cmap2, extend='both')
axs.coastlines()
axs.add_feature(feature.OCEAN, zorder=100, edgecolor='k')
axs.set_extent(bbox, projection)
cbar = fig.colorbar(cs, ax=axs, orientation='vertical')
cbar.set_label('Land-use')
fig.subplots_adjust(bottom=0.05, top=0.95, left=0.01, right=0.99, wspace=0.02, hspace=0.2)
plt.show()
When using Mercator() as both transform and projection, the map shows but without the black lines and with the wrong ocean colour:
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,:,:]
bbox = [2.5,20,57, 71.5]
projection = crs.Mercator()
transform = crs.Mercator()
nrows = 1
ncolumns = 1
vmin = 0
vmax = 24
levels = np.linspace(vmin, vmax, 24+1)
fig, axs = plt.subplots(nrows = nrows, ncols = ncolumns,
subplot_kw = {'projection': projection},
figsize = (11,8.5))
cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list("my_colormap", ('#00ffdd', '#acb20c', '#e8a202', '#ffff00', '#ff0000',
'#ff7b59', '#ffb29f', '#bf0041', '#e16173', '#784b04',
'#0e6b1d', '#00b737', '#09ff00', '#81d41a', '#afd095',
'#16a69a', '#55215b', '#6c2dcc', '#8e86ae', '#000000',
'#333333', '#666666', '#999999', '#eeeeee'),
N=24, gamma=1.0)
cs = axs.contourf(lons, lats, LU, 60, transform=transform, levels=levels, cmap=cmap2, extend='both')
axs.coastlines()
axs.add_feature(feature.OCEAN, zorder=100, edgecolor='k')
axs.set_extent(bbox, projection)
cbar = fig.colorbar(cs, ax=axs, orientation='vertical')
cbar.set_label('Land-use')
fig.subplots_adjust(bottom=0.05, top=0.95, left=0.01, right=0.99, wspace=0.02, hspace=0.2)
plt.show()
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,:,:]
bbox = [2.5,20,57, 71.5]
projection = crs.Mercator()
transform = crs.PlateCarree()
nrows = 1
ncolumns = 1
vmin = 0
vmax = 24
levels = np.linspace(vmin, vmax, 24+1)
fig, axs = plt.subplots(nrows = nrows, ncols = ncolumns,
subplot_kw = {'projection': projection},
figsize = (11,8.5))
cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list("my_colormap", ('#00ffdd', '#acb20c', '#e8a202', '#ffff00', '#ff0000',
'#ff7b59', '#ffb29f', '#bf0041', '#e16173', '#784b04',
'#0e6b1d', '#00b737', '#09ff00', '#81d41a', '#afd095',
'#16a69a', '#55215b', '#6c2dcc', '#8e86ae', '#000000',
'#333333', '#666666', '#999999', '#eeeeee'),
N=24, gamma=1.0)
cs = axs.contourf(lons, lats, LU, 60, transform=transform, levels=levels, cmap=cmap2, extend='both')
axs.coastlines()
axs.add_feature(feature.OCEAN, zorder=100, edgecolor='k')
axs.set_extent(bbox, projection)
cbar = fig.colorbar(cs, ax=axs, orientation='vertical')
cbar.set_label('Land-use')
fig.subplots_adjust(bottom=0.05, top=0.95, left=0.01, right=0.99, wspace=0.02, hspace=0.2)
plt.show()
What am I misunderstanding here? How can I solve the issue and display my data on the Mercator (or any other non-PlateCarree) projection?