pythonmatplotlibcartopymap-projections

How to plot Cartopy pcolormesh with Robinson projection?


I have a simple piece of code and satellite data that works as intended with Cartopy's default projection "PlateCarree":

import cartopy.crs as ccrs
from cartopy import feature
from netCDF4 import Dataset
import matplotlib.pyplot as plt

#get data
f = Dataset('data/matt.nc')
lats = f['lat'][:]
lons = f['lon'][:]
x = f['aot_869'][:]

#plot
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(feature.COASTLINE, edgecolor='grey')
plt.pcolormesh(lons, lats, x, transform=ccrs.PlateCarree())
plt.show()

PlateCarree projection

But when I change the projection to anything else, including "Robinson", the coastline disappears and the satellite data remains in the PlateCarree projection.

#plot
ax = plt.axes(projection=ccrs.Robinson())
ax.add_feature(feature.COASTLINE, edgecolor='grey')
plt.pcolormesh(lons, lats, x, transform=ccrs.Robinson())
plt.show()

Robinson projection


Solution

  • plt.axes needs to be in a Robinson Projection while plt.pcolormesh needs to be in a PlateCarree projection for the overall figure to be in Robinson. As swatchai explains in the comments, "in pcolormesh() you must declare the coordinate system of (lons, lats) with the statement transform=?? correctly. If (lons, lats) are in simple decimal degrees, you always use transform=ccrs.PlateCarree() no matter what the plotting projection is."

    import cartopy.crs as ccrs
    from cartopy import feature
    from netCDF4 import Dataset
    import matplotlib.pyplot as plt
    
    #get data
    f = Dataset('data/matt.nc')
    lats = f['lat'][:]
    lons = f['lon'][:]
    x = f['aot_869'][:]
    
    #plot
    ax = plt.axes(projection=ccrs.Robinson())
    ax.add_feature(feature.COASTLINE, edgecolor='grey')
    plt.pcolormesh(lons, lats, x, transform=ccrs.PlateCarree())
    plt.show()