赞
踩
绘制西太年平均温度空间分布图
在NASA官网 下载在分析的海表温度数据
下载地址(https://psl.noaa.gov/data/gridded/data.noaa.ersst.v5.html)
代码如下:
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter # 导入经纬度格式器
代码如下(示例):
# 加载NetCDF格式的SST数据(替换为你的SST数据文件路径) ds = xr.open_dataset('C:\\Users\\huain\\Desktop\\BY\\sst.nc') # 假设你的SST数据在'sst.nc'文件中 # 选择SST变量(替换为你的SST变量名) sst = ds['sst'] # 假设SST变量名为'sst' # 计算时间轴上的平均值(如果时间是一个维度) sst_mean = sst.mean(dim='time') # 假设'time'是时间维度 # 创建一个地图并设置投影 fig = plt.figure(figsize=(10, 5)) ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree()) # 添加陆地和海洋特征 ax.add_feature(cfeature.LAND, color='lightgray') ax.add_feature(cfeature.OCEAN, color='w', edgecolor='lightgray') ax.coastlines(color='black') # 绘制SST平均值数据 sst_plot = sst_mean.plot.contourf(ax=ax, transform=ccrs.PlateCarree(), cmap='viridis', levels=30, extend='both', add_colorbar=False) # levels参数可以调整等温线的数量 # 添加颜色条 cbar = fig.colorbar(sst_plot, drawedges=True, ax=ax, location='right', shrink=0.95, pad=0.01, spacing='uniform', label='Average Sea Surface Temperature (°C)') cbar.ax.tick_params(labelsize=10) # 设置色标尺标签大小 # 添加等温线 sst_contour = sst_mean.plot.contour(ax=ax, transform=ccrs.PlateCarree(), colors='gray', levels=20) # 为等值线添加标签 plt.clabel(sst_contour, inline=True, fontsize=10, fmt='%1.1f') # 添加一个采样点位(129°E,17°N) sample_point = ax.plot(129, 17, 'ro', transform=ccrs.PlateCarree(), markersize=8) # 为采样点添加标签 ax.annotate('WPS-1/2', xy=(129, 17), xytext=(20, 20), textcoords='offset points', ha='left', va='bottom', bbox=dict(boxstyle='round', fc='w', ec='0.5', lw=2), arrowprops=dict(facecolor='black', arrowstyle='->'), transform=ccrs.PlateCarree()) # 设置地图的经纬度范围(可选) ax.set_extent([100, 180, -30, 50], crs=ccrs.PlateCarree()) # 添加网格线 gl = ax.gridlines(draw_labels=True, linewidths=2, color='gray', alpha=0.5, linestyle='-') gl.xlabels_top = False gl.ylabels_right = False gl.xformatter = LongitudeFormatter() # 使用默认的经度格式器 gl.yformatter = LatitudeFormatter() # 使用默认的纬度格式器 gl.xlabel_style = {'size': 10, 'color': 'black'} gl.ylabel_style = {'size': 10, 'color': 'black'} # 显示地图 plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。