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

如何在python中使用imshow绘制时获取国家边界线,python,matplotlib,netcdf,python-xarray,imshow,Python,Matplotlib,Netcdf,Python Xarray,Imshow,我试图使用xarray和matplotlib从netcdf文件中绘制季节平均值。我得到的是纬度和经度轴的图,但没有国家边界线。如何获取地块上的国家边界线 import xarray as xr import matplotlib as mpl import matplotlib.pyplot as plt fname='/home/atmosphere/data/outputs/2010.nc' #<<<<<<<<<<

我试图使用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')
使用Cartopy:。