1 import numpy as np, matplotlib.pyplot as plt; from _utils.palette_maps import palette_cmap, palette_stops, contrast_text 2 import seaborn as sns 3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS 4 setup_style() 5 np.random.seed(42) 6 7 regions = ['北京', '上海', '广东', '浙江', '江苏', '四川', '湖北', '山东'] 8 years = [str(y) for y in range(2015, 2024)] 9 data = np.random.uniform(3, 12, (len(regions), len(years))) 10 data = np.round(data, 1) 11 12 fig, ax = plt.subplots(figsize=(9, 5)) 13 sns.heatmap(data, annot=True, fmt='.1f', cmap=palette_cmap('sequential'), 14 xticklabels=years, yticklabels=regions, 15 linewidths=0.3, linecolor='white', 16 cbar_kws={'label': 'GDP增长率 (%)', 'shrink': 0.8}, ax=ax) 17 ax.set_xlabel('年份', fontsize=11); ax.set_ylabel('地区', fontsize=11) 18 ax.set_yticklabels(regions, rotation=0, fontsize=9) 19 [t.set_color(contrast_text(ax.collections[0].cmap(ax.collections[0].norm(float(t.get_text()))))) for t in ax.texts]; fig.tight_layout() 20 save_fig(fig, 'figures/fig_spatiotemporal.pdf')