| import numpy as np |
| import matplotlib.pyplot as plt |
| import cartopy.crs as ccrs |
| from matplotlib.colors import BoundaryNorm |
| from matplotlib.cm import ScalarMappable |
|
|
| |
| 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) |
|
|
| |
| |
| 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 |
|
|
| |
| 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) |
|
|
| |
| 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) |
|
|
| |
| sm = ScalarMappable(norm=norm, cmap=cmap) |
| cbar = fig.colorbar(sm, ax=ax, orientation='horizontal', |
| fraction=0.05, pad=0.04, aspect=25) |
|
|
| |
| ticks = np.linspace(levels[0], levels[-1], num=6) |
| cbar.set_ticks(ticks) |
| cbar.ax.tick_params(labelsize=7) |
| |
| |
| |
| |
|
|
| |
| plt.savefig("era5_like_cartopy_layout.pdf", dpi=100, bbox_inches="tight") |
|
|