File size: 3,394 Bytes
849505e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from matplotlib.colors import BoundaryNorm
from matplotlib.cm import ScalarMappable

# ========== 1. Generate random global-like data ==========
np.random.seed(42)
nlon, nlat, nt = 64, 32, 5
lon = np.linspace(-180, 180, nlon)
lat = np.linspace(-90, 90, nlat)

true_state = 0.012 + 0.004 * np.sin(np.deg2rad(lat[:, None])) * np.cos(np.deg2rad(lon[None, :]/2))
true_state = np.stack([true_state + 0.001*np.random.randn(nlat, nlon) for _ in range(nt)], axis=0)
pred_state = true_state + 0.0015 * np.random.randn(nt, nlat, nlon)
abs_err = np.abs(pred_state - true_state)

# ========== 2. Layout configuration ==========
# 删除 Observations 行
row_labels = ["True State", "Tensor-Var Result", "Absolute Error"]
col_times = ["2018-01-01\n00:00", "2018-01-02\n00:00", "2018-01-03\n00:00", "2018-01-04\n00:00", "2018-01-05\n00:00"]

nrows, ncols = len(row_labels), len(col_times)
fig, axes = plt.subplots(
    nrows, ncols, figsize=(18, 8),
    subplot_kw={'projection': ccrs.Robinson()},
    constrained_layout=False
)

# 增加列距和行距
plt.subplots_adjust(wspace=0.15, hspace=0.15)

cmap_main = plt.cm.RdBu_r
cmap_error = plt.cm.Blues

vmin_main, vmax_main = 0.005, 0.02
vmin_err, vmax_err = 0.0, 0.004

# ========== 3. Plot each panel ==========
for i in range(nrows):
    for j in range(ncols):
        ax = axes[i, j]
        ax.coastlines(linewidth=0.4)
        ax.gridlines(draw_labels=False, color='gray', linestyle=':', alpha=0.3)

        if i == 0:
            data = true_state[j]
            levels = np.linspace(vmin_main, vmax_main, 15)
            cmap = cmap_main
            norm = BoundaryNorm(levels, cmap.N)
        elif i == 1:
            data = pred_state[j]
            levels = np.linspace(vmin_main, vmax_main, 15)
            cmap = cmap_main
            norm = BoundaryNorm(levels, cmap.N)
        elif i == 2:
            data = abs_err[j]
            levels = np.linspace(vmin_err, vmax_err, 10)
            cmap = cmap_error
            norm = BoundaryNorm(levels, cmap.N)

        im = ax.contourf(lon, lat, data, levels=levels, cmap=cmap,
                         transform=ccrs.PlateCarree(), extend='both')

        # 列标题
        if i == 0:
            ax.set_title(col_times[j], fontsize=14, fontweight='bold', pad=20)

        # 行标签(旋转90°加粗放在行左侧中间)
        if j == 0:
            axes[i, 0].text(-0.12, 0.5, row_labels[i],
                            transform=axes[i, 0].transAxes,
                            ha='center', va='center',
                            fontsize=12, fontweight='bold',
                            rotation=90)

        # 每个子图单独添加 colorbar(横向放在下面)
        sm = ScalarMappable(norm=norm, cmap=cmap)
        cbar = fig.colorbar(sm, ax=ax, orientation='horizontal',
                            fraction=0.05, pad=0.04, aspect=25)

        # 调整 colorbar tick 间隔
        ticks = np.linspace(levels[0], levels[-1], num=6)  # 只显示6个刻度
        cbar.set_ticks(ticks)
        cbar.ax.tick_params(labelsize=7)
        # if i == 2:
        #     cbar.set_label('Absolute Error', fontsize=8)
        # else:
        #     cbar.set_label('Value', fontsize=8)

# ========== 4. Save figure ==========
plt.savefig("era5_like_cartopy_layout.pdf", dpi=100, bbox_inches="tight")