Chen
Chen

Reputation: 39

Lack of Projection for Cartopy Contour

I'm trying to put some data onto a contourmap via cartopy. However, after plotting the data, the projection still seems to be off. The surface_temp.X and surface_temp.Y are lat/lon, while masked_fill is the actual data values. This seems to have worked in basemap, but I'm not sure why it doesn't in cartopy.

Cartopy:

fig = plt.figure(figsize=(12,4.76), dpi=100)
fig.clf()
ax = plt.axes(projection=ccrs.Mercator())
ax.coastlines()
ax.contourf(surface_temp.X, surface_temp.Y, surface_temp.masked_fill[:], latlon = 'true', transform = ccrs.Mercator())
plt.show()

Basemap:

fig = plt.figure(figsize=(15,4.76), dpi=100)
        fig.clf()
        plt.axes([0,0,1,1], frameon=False)
        plt.title(title)
        m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80, llcrnrlon=0,urcrnrlon=360,lat_ts=20,resolution='c')
m.contourf(surface_temp.X, surface_temp.Y, surface_temp.masked_fill[:], latlon = 'true')

Basemap Result:

Basemap

Cartopy Result (Contour commented out):

Cartopy - Merc

Cartopoy Result (Contour)

Cartopy - Contour

Upvotes: 0

Views: 827

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339102

The paradigm of cartopy seems to be to always work on lat/lon coordinates. This means, you should not transform your data according to the projection, but stay in lat/lon.

Hence, instead of

ax.contourf(..., transform = ccrs.Mercator())

you would need

ax.contourf(..., transform = ccrs.PlateCarree())

A complete example:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.examples.waves import sample_data

ax = plt.axes(projection=ccrs.Mercator())

lons, lats, data = sample_data(shape=(20, 40))

ax.contourf(lons, lats, data, transform=ccrs.PlateCarree())

ax.coastlines()
ax.gridlines()

plt.show()

enter image description here

Upvotes: 1

Related Questions