import matplotlib as mpl
%matplotlib inline
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
import xarray as xr
import rioxarray as rxr
# 基础轮廓
fig, ax = plt.subplots(1, 1, subplot_kw={'projection': ccrs.PlateCarree()})
extent = (106, 127, 17, 41)
ax.set_extent(extent,crs=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND,facecolor='grey')
ax.add_feature(cfeature.OCEAN,facecolor='lightblue')
ax.add_feature(cfeature.COASTLINE,edgecolor='DarkSlateGray')
# 读取高程图
file_path="ETOPO_2022_v1_60s_N90W180_bed_coast.tif"
dem_ds=rxr.open_rasterio(file_path)
data0 = dem_ds[0]
data0=xr.where(data0<0,-data0,np.nan)
lons=dem_ds.x.values
lats=np.flip(dem_ds.y.values) # 倒转纬度
# lats=dem_ds.y.values[::-1, :] # 另一种方法
# 添加等高线
lon, lat = np.meshgrid(lons, lats)
data_values = [500]
cs = ax.contour(lon, lat, data0[::-1, :], levels=data_values)
ax.clabel(cs, inline=True, fontsize=18, fmt='%1.0f', inline_spacing=20)
plt.show()
rioxarray读取栅格文件 画等高线图时纬度翻转
最新推荐文章于 2024-07-23 14:36:35 发布