简体   繁体   中英

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:

底图

Cartopy Result (Contour commented out):

Cartopy-Merc

Cartopoy Result (Contour)

Cartopy-轮廓

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM