简体   繁体   English

使用 NorthPolarStereo 用 cartopy 绘制网格数据

[英]Plotting gridded data with cartopy using NorthPolarStereo

I'm moving from Basemap to Cartopy and want to plot data for the Arctic Ocean that covers the pole.我正在从 Basemap 转移到 Cartopy,并且想要绘制覆盖极地的北冰洋的数据。

I've decided to use the NorthPolarStereo() projection and am happy to use either pcolormesh or contourf.我决定使用 NorthPolarStereo() 投影并且很高兴使用 pcolormesh 或 contourf。 Unfortunately my field of data doesn't show up when I execute the following code:不幸的是,当我执行以下代码时,我的数据字段没有显示:

import cartopy.crs as ccrso
from netCDF4 import Dataset


def import_envisat_field(year,
                         month):

    data_dir = f'/media/robbie/Seagate Portable Drive/Envisat_thickness/{year}/'
    file = f'ESACCI-SEAICE-L3C-SITHICK-RA2_ENVISAT-NH25KMEASE2-{year}{month}-fv2.0.nc'
    data = Dataset(data_dir+file)

    return(data)

# Import data

data = import_envisat_field("2003","02")

# Make plot

fig = plt.figure(figsize=[10, 5])
ax = plt.axes(projection=ccrs.NorthPolarStereo())

ax.add_feature(cartopy.feature.OCEAN, zorder=0)
ax.add_feature(cartopy.feature.LAND, zorder=1, edgecolor='black')

extent = 2500000

ax.set_extent((-extent,
               extent,
               -extent,
               extent),
              crs=ccrs.NorthPolarStereo())

ax.gridlines()

lon = np.array(data['lon'])
lat = np.array(data['lat'])
field = np.array(data['sea_ice_thickness'])[0]

print(lon.shape,lat.shape,field.shape)
# This print command gives (432, 432) (432, 432) (432, 432)

plt.pcolormesh(lon, lat, field,zorder=2,
             transform=ccrs.NorthPolarStereo())

plt.show()

The data plots in a straightforward way using Basemap, but executing the above code just gives me a nice picture of the Arctic ocean but without my data on it.使用 Basemap 以一种直接的方式绘制数据,但执行上述代码只是给了我一张北冰洋的漂亮图片,但没有我的数据。

I've also tried replacing plt.pcolormesh with ax.pcolormesh but that didn't work either.我也试过用 ax.pcolormesh 替换 plt.pcolormesh 但这也不起作用。

Cartopy output: Cartopy 输出:

我从 Cartopy 中得到了什么

Basemap output with the same data:具有相同数据的底图输出:

我用底图得到了什么

If your data coordinates are latitude and longitude you need to use the PlateCarree transform:如果您的数据坐标是纬度和经度,则需要使用 PlateCarree 变换:

plt.pcolormesh(lon, lat, field,zorder=2, transform=ccrs.PlateCarree())

The transform describes the data coordinates and is independent from the projection you'd like to plot on.变换描述了数据坐标并且独立于您想要绘制的投影。 See this guide in the Cartopy documentation for more details https://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html有关更多详细信息,请参阅 Cartopy 文档中的本指南https://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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