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

2024-07-05 10:51:27 发布

您现在位置: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)

Tags: importdatatimematplotlibasdsslice国家
1条回答
网友
1楼 · 发布于 2024-07-05 10:51:27

正如@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')

相关问题 更多 >