1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten
   4 setup_style()
   5 
   6 years = np.arange(2015, 2025)
   7 data = {
   8     '线上渠道': np.array([15, 20, 28, 35, 42, 48, 55, 60, 65, 68]),
   9     '线下门店': np.array([60, 55, 48, 42, 38, 35, 30, 28, 25, 22]),
  10     '其他渠道': np.array([25, 25, 24, 23, 20, 17, 15, 12, 10, 10]),
  11 }
  12 
  13 fig, ax = plt.subplots(figsize=(8, 5))
  14 
  15 # 渐变填充层叠
  16 cumsum = np.zeros(len(years))
  17 for i, (name, vals) in enumerate(data.items()):
  18     # 多层渐变填充
  19     for layer, alpha in enumerate([0.35, 0.20, 0.10]):
  20         ax.fill_between(years, cumsum + layer * 0.3, cumsum + vals - layer * 0.3,
  21                         alpha=alpha, color=PALETTE[i], linewidth=0)
  22     # 原色边框轮廓线
  23     ax.plot(years, cumsum + vals, color=PALETTE[i], linewidth=1.5, alpha=0.8, label=name)
  24     cumsum += vals
  25 
  26 # 事件标记线
  27 events = {2020: '疫情爆发', 2022: '政策调整'}
  28 for year, event in events.items():
  29     ax.axvline(x=year, color=COLORS['ref_line'], linestyle=':', linewidth=1, alpha=0.6)
  30     ax.text(year + 0.1, cumsum.max() * 0.95, event, fontsize=8, color=COLORS['ref_line'],
  31             rotation=90, va='top', ha='left',
  32             bbox=dict(boxstyle='round,pad=0.2', facecolor='white',
  33                       edgecolor=COLORS['ref_line'], alpha=0.8, linewidth=0.5))
  34 
  35 # 端点数值标注
  36 cumsum_end = np.zeros(1)
  37 for i, (name, vals) in enumerate(data.items()):
  38     y_pos = cumsum_end + vals[-1] / 2
  39     ax.text(years[-1] + 0.3, y_pos[0], f'{vals[-1]}%',
  40             fontsize=8, color=PALETTE[i], fontweight='bold', va='center',
  41             bbox=dict(boxstyle='round,pad=0.15', facecolor='white',
  42                       edgecolor=PALETTE[i], alpha=0.8, linewidth=0.5))
  43     cumsum_end += vals[-1]
  44 
  45 ax.set_xlabel('年份', fontsize=11)
  46 ax.set_ylabel('占比 (%)', fontsize=11)
  47 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, loc='best')
  48 ax.set_xlim(years[0], years[-1] + 0.8)
  49 ax.grid(alpha=0.12, linestyle='--', color=COLORS['grid'])
  50 ax.spines['top'].set_visible(False)
  51 ax.spines['right'].set_visible(False)
  52 fig.tight_layout()
  53 save_fig(fig, 'figures/fig_area.pdf')