当前位置:   article > 正文

Python绘制海表温度_python绘制海温图

python绘制海温图


前言

绘制西太年平均温度空间分布图


一、数据准备

在NASA官网 下载在分析的海表温度数据
下载地址(https://psl.noaa.gov/data/gridded/data.noaa.ersst.v5.html)

二、代码编写

1.引入库

代码如下:

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  # 导入经纬度格式器 
  • 1
  • 2
  • 3
  • 4
  • 5

2.读入数据并绘图

代码如下(示例):

# 加载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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

绘图结果

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/856691?site
推荐阅读
相关标签
  

闽ICP备14008679号