1 import numpy as np, matplotlib.pyplot as plt
   2 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   3 setup_style()
   4 
   5 features = ['人均GDP', '城镇化率', '教育支出', '医疗投入', '交通密度',
   6             '产业结构', '人口密度', '绿化覆盖', '科技投入', '能源消耗']
   7 importance = np.array([0.182, 0.156, 0.134, 0.112, 0.098, 0.087, 0.076, 0.065, 0.054, 0.036])
   8 sort_idx = np.argsort(importance)
   9 features = [features[i] for i in sort_idx]
  10 importance = importance[sort_idx]
  11 
  12 n = len(features)
  13 fig, ax = plt.subplots(figsize=(7, max(4, n*0.45)))
  14 y = np.arange(n)
  15 max_val = importance.max()
  16 
  17 for i in range(n):
  18     ratio = importance[i] / max_val
  19     c = _lighten(PALETTE[0], 0.5 * (1 - ratio))
  20     ax.barh(y[i], importance[i], height=0.6,
  21             color=_lighten(c, 0.3), edgecolor=c, linewidth=1.2, zorder=3)
  22     ax.text(importance[i] + max_val*0.02, y[i], f'{importance[i]:.3f}',
  23             va='center', ha='left', fontsize=9, fontweight='bold', color=c)
  24 
  25 ax.set_yticks(y); ax.set_yticklabels(features, fontsize=10)
  26 ax.set_xlabel('特征重要性', fontsize=11)
  27 ax.set_xlim(0, max_val * 1.2)
  28 ax.spines['top'].set_visible(False); ax.spines['right'].set_visible(False)
  29 ax.grid(axis='x', alpha=0.12, linestyle='--')
  30 fig.tight_layout()
  31 save_fig(fig, 'figures/fig_feature_importance.pdf')