1 import numpy as np; from _utils.palette_maps import palette_cmap, palette_stops, contrast_text 2 import matplotlib.pyplot as plt 3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 4 5 setup_style() 6 np.random.seed(42) 7 8 # === 模拟数据:60 个月 × 40 个经度 === 9 n_time = 60; n_space = 40 10 months = np.arange(n_time) 11 lons = np.linspace(120, 280, n_space) # 太平洋经度 12 # 季节波动 + 空间波形 + ENSO 信号 + 噪声 13 seasonal = 0.6 * np.sin(2 * np.pi * months[:, None] / 12) 14 spatial = 0.3 * np.cos(2 * np.pi * (lons - 180) / 80)[None, :] 15 enso = np.zeros((n_time, n_space)) 16 # 模拟两个事件 17 enso[15:25, 10:30] += 1.8 * np.exp(-((np.arange(10)[:, None] - 5)**2 + (np.arange(20)[None, :] - 10)**2) / 30) 18 enso[40:48, 20:35] -= 1.3 * np.exp(-((np.arange(8)[:, None] - 4)**2 + (np.arange(15)[None, :] - 7)**2) / 25) 19 data = seasonal + spatial + enso + np.random.normal(0, 0.18, (n_time, n_space)) 20 21 fig, ax = plt.subplots(figsize=(7, 5)) 22 23 # ★ 双向色阶(偏离零)— 用 coolwarm 但反转让暖色=正 24 v = np.nanmax(np.abs(data)) 25 im = ax.imshow(data, aspect='auto', cmap=palette_cmap('diverging'), vmin=-v, vmax=v, 26 extent=[lons[0], lons[-1], months[-1], months[0]], 27 interpolation='bilinear') 28 29 # ★ 零线等高线(可选,帮助读者定位"基线") 30 cs = ax.contour(lons, months, data, levels=[0], colors=[COLORS['ref_line']], 31 linewidths=0.6, alpha=0.5, linestyles='--') 32 33 # ★ 事件标注:用半透明矩形圈出 34 from matplotlib.patches import Rectangle 35 ax.add_patch(Rectangle((lons[10], 15), lons[30] - lons[10], 10, 36 fill=False, edgecolor=COLORS['highlight'], 37 linewidth=1.4, linestyle='-', zorder=5)) 38 ax.text(lons[20], 12, 'El Niño 期', fontsize=8, ha='center', 39 color=COLORS['highlight'], fontweight='bold', 40 bbox=dict(boxstyle='round,pad=0.2', facecolor='white', 41 edgecolor=COLORS['highlight'], alpha=0.9, linewidth=0.6)) 42 43 ax.add_patch(Rectangle((lons[20], 40), lons[35] - lons[20], 8, 44 fill=False, edgecolor=PALETTE[0], 45 linewidth=1.4, linestyle='-', zorder=5)) 46 ax.text(lons[27], 53, 'La Niña 期', fontsize=8, ha='center', 47 color=PALETTE[0], fontweight='bold', 48 bbox=dict(boxstyle='round,pad=0.2', facecolor='white', 49 edgecolor=PALETTE[0], alpha=0.9, linewidth=0.6)) 50 51 ax.set_xlabel('经度 (°E)', fontsize=10) 52 ax.set_ylabel('时间(月)', fontsize=10) 53 ax.tick_params(labelsize=9) 54 55 # ★ 右侧 colorbar 56 cbar = fig.colorbar(im, ax=ax, shrink=0.7, pad=0.04, aspect=20) 57 cbar.set_label('SST 距平 (°C)', fontsize=9) 58 cbar.ax.tick_params(labelsize=8) 59 cbar.outline.set_linewidth(0.4) 60 cbar.outline.set_edgecolor(COLORS['grid']) 61 62 # 顶部加经度参考标记 63 ax.axvline(180, color=COLORS['ref_line'], linewidth=0.5, alpha=0.4, linestyle=':') 64 ax.text(180, -1.5, '日期变更线', fontsize=7, ha='center', 65 color=COLORS['ref_line'], style='italic') 66 67 fig.tight_layout() 68 save_fig(fig, 'figures/fig_hovmoller.pdf')