如何在 python 中使用 imshow 绘图时获取国家边界线

How to get country border lines while plotting with imshow in python

我正在尝试使用 xarray 和 matplotlib 从 netcdf 文件中绘制季节性平均值。我得到了带有纬度和经度轴的图,但没有国家边界线。如何在地块上获得国家边界线。

import xarray as xr
import matplotlib as mpl
import matplotlib.pyplot as plt

fname='/home/atmosphere/data/outputs/2010.nc'         #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

ds=xr.open_mfdataset(fname)
varlist=list(ds.variables)
imr=ds.sel(lat=slice(0,35),lon=slice(60,100)) #subsetting overthe region
imrbt=imr['temp']                             #making into a data array  
ds['time.season']

seasonal=imrbt.groupby('time.season').mean(dim='time')
seasonal.plot.imshow(col='season',robust=True)

如@Bart 所述,您必须使用 cartopy 来解决此问题:

import cartopy.crs as ccrs
air = xr.tutorial.open_dataset('air_temperature').air
ax = plt.axes(projection=ccrs.Orthographic(-80, 35))
seasonal.plot.contourf(ax=ax, transform=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.BORDERS)
ax.coastlines()

要以更高分辨率添加国家/地区边界,您必须使用 cartopy 功能:

import cartopy.feature as cfeature

country_borders = cfeature.NaturalEarthFeature(
    category='cultural',
    name='‘admin_0_boundary_lines_land',
    scale='50m',
    facecolor='none')
ax.add_feature(country_borders, edgecolor='gray')