0

I'm trying to migrate from Basemap to Cartopy looking demo examples. I have a simple code using both coastlines() and contourf(). I can get both separately but not simultaneously. The data set is a netcdf file containing the sea surface temperature data of the west Med. The code is:

   import numpy as np
   from netCDF4 import Dataset
   import cartopy
   import matplotlib.pyplot as plt

   # DATA
   data = Dataset('20190715.0504.n19.nc','r')
   lon = data.variables['lon'][:]
   lat = data.variables['lat'][:]
   sst = data.variables['mcsst'][0,:,:].squeeze()
   xxT,yyT = np.meshgrid(lon,lat)

   # PLOT
   fig = plt.figure(figsize=(10, 5))
   ax1 = fig.add_axes([0.01,0.01,0.98,0.98],projection=cartopy.crs.Mercator())
   ax1.coastlines()
   #ax1.contourf(xxT,yyT,sst)
   ax1.set_extent([16.5, -15.0, 35.0, 46.5])

   plt.show()

With this code I get:

enter image description here

If I use:

   #ax1.coastlines()
   ax1.contourf(xxT,yyT,sst)
   ax1.set_extent([16.5, -15.0, 35.0, 46.5])

I get a white rectangle.

If I use:

   #ax1.coastlines()
   ax1.contourf(xxT,yyT,sst)
   ax1.set_extent([16.5,-15.0,35.0,46.5],crs=cartopy.crs.Mercator())

I get the contoured data.

enter image description here

But with both:

   ax1.coastlines()
   ax1.contourf(xxT,yyT,sst)
   ax1.set_extent([16.5,-15.0,35.0,46.5],crs=cartopy.crs.Mercator())

the contour is ok ! but without coastlines. And if finally

   ax1.coastlines()
   ax1.contourf(xxT,yyT,sst)
   ax1.set_extent([16.5,-15.0,35.0,46.5])

only coastlines are shown, not contour !. I try to understand how I have to proceed because problems arose when trying to include this into a GUI with options show/hide for coatlines, features, etc. Just in case I'm using Python 3.7.4, Cartopy 0.17, proj4 5.2, matplotlib 3.1.1. Thanks !

user1259970
  • 333
  • 3
  • 14
  • Just a wild guess, `ax1.contourf(xxT,yyT,sst,transform=cartopy.crs.Geodetic())` will transform the coordinates to match the extents and projection you use. – swatchai Feb 06 '20 at 17:25
  • It does not work. It raises: File "/anaconda3/lib/python3.7/site-packages/cartopy/mpl/geoaxes.py", line 1385, in contourf raise ValueError('invalid transform:' ValueError: invalid transform: Spherical contouring is not supported - consider using PlateCarree/RotatedPole but with cartopy.crs.PlateCarree() is ok !!!!!!! – user1259970 Feb 07 '20 at 11:05

1 Answers1

1

Thanks to swatchai suggestion, although, I still don't understand why I need to use the transform keyword with the specific PlateCarree projection keyword, the code works fine if:

fig = plt.figure(figsize=(10, 5))
ax1 = fig.add_axes([0.01, 0.01, 0.98, 0.98],projection=cartopy.crs.Mercator())
ax1.coastlines('10m')
ax1.set_extent([16.5, -15.0, 35.0, 46.5])
ax1.contourf(xxT,yyT,sst,transform=cartopy.crs.PlateCarree())

Here the result: enter image description here

user1259970
  • 333
  • 3
  • 14