如何用Python绘制NOAA风格的动态SST海表温度分布图?
绘制NOAA风格海表温度(SST)分布图及动画指南
一、静态图绘制(匹配NOAA样式)
NOAA的SST图核心特征包括海洋专属色标、清晰的地理底图、固定色阶范围,可以用matplotlib+cartopy+cmocean实现:
- 依赖安装
pip install matplotlib cartopy cmocean
- 核心代码示例
import cartopy.crs as ccrs import cartopy.feature as cfeature import matplotlib.pyplot as plt import cmocean import numpy as np # 假设已加载lon(经度数组)、lat(纬度数组)、sst_data(单时间点SST数据) sst_min = np.nanmin(sst_data) sst_max = np.nanmax(sst_data) # 设置NOAA常用的PlateCarree投影 fig, ax = plt.subplots(figsize=(12, 6), subplot_kw={'projection': ccrs.PlateCarree()}) # 添加地理要素:海岸线、陆地填充、国界线 ax.coastlines(resolution='50m', color='darkgray', linewidth=0.7) ax.add_feature(cfeature.LAND, facecolor='#f0f0f0') ax.add_feature(cfeature.BORDERS, linewidth=0.5, color='gray') # 绘制SST数据,使用匹配NOAA风格的海洋色标 im = ax.pcolormesh(lon, lat, sst_data, cmap=cmocean.cm.thermal, transform=ccrs.PlateCarree(), vmin=sst_min, vmax=sst_max) # 添加底部色条,对齐NOAA布局 cbar = plt.colorbar(im, orientation='horizontal', pad=0.08, aspect=60) cbar.set_label('Sea Surface Temperature (°C)', fontsize=10) cbar.ax.tick_params(labelsize=8) # 设置标题与排版 ax.set_title('Sea Surface Temperature (NOAA Style)', fontsize=12) plt.tight_layout() plt.show()
二、生成SST变化动画
基于静态图框架,用matplotlib.FuncAnimation实现逐帧更新:
from matplotlib.animation import FuncAnimation # 假设sst_timeseries是按时间维度存储的SST数据,shape为(时间步数, 纬度数, 经度数) time_steps = sst_timeseries.shape[0] # 固定全局色阶范围,避免动画帧间色标波动 global_vmin = np.nanmin(sst_timeseries) global_vmax = np.nanmax(sst_timeseries) fig, ax = plt.subplots(figsize=(12, 6), subplot_kw={'projection': ccrs.PlateCarree()}) ax.coastlines(resolution='50m', color='darkgray', linewidth=0.7) ax.add_feature(cfeature.LAND, facecolor='#f0f0f0') ax.add_feature(cfeature.BORDERS, linewidth=0.5, color='gray') # 初始化第一帧 im = ax.pcolormesh(lon, lat, sst_timeseries[0], cmap=cmocean.cm.thermal, transform=ccrs.PlateCarree(), vmin=global_vmin, vmax=global_vmax) cbar = plt.colorbar(im, orientation='horizontal', pad=0.08, aspect=60) cbar.set_label('Sea Surface Temperature (°C)', fontsize=10) title = ax.set_title(f'SST Distribution - Year {2000}', fontsize=12) # 定义帧更新函数 def update(frame): # 更新当前帧的SST数据 im.set_array(sst_timeseries[frame].flatten()) # 更新标题(假设时间序列从2000年开始) title.set_text(f'SST Distribution - Year {2000 + frame}') return im, title # 创建动画,设置帧间隔为400ms ani = FuncAnimation(fig, update, frames=time_steps, interval=400, blit=True) # 保存为MP4格式(需提前安装ffmpeg) ani.save('sst_yearly_animation.mp4', writer='ffmpeg', dpi=100)
三、关键细节调整
- 色标一致性:动画必须固定
vmin和vmax,否则帧间色标变化会误导观测 - 陆地掩膜:如果原始数据包含陆地值,可通过
sst_data[sst_data > 35] = np.nan(根据实际数据阈值)过滤,或用cartopy的LAND层覆盖 - 分辨率优化:海岸线分辨率选
50m或10m可提升细节,匹配NOAA图精度
内容的提问来源于stack exchange,提问作者rach
相关产品推荐
相关产品推荐

