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(2016, 2025) 7 bar_data = np.array([120, 145, 168, 195, 230, 260, 310, 355, 400]) 8 line_data = np.array([15.2, 18.5, 22.1, 25.8, 30.2, 35.5, 38.1, 42.3, 45.0]) 9 10 fig, ax1 = plt.subplots(figsize=(8, 5.5)) 11 ax2 = ax1.twinx() 12 13 # ★ 淡色填充 + 原色边框柱 14 bars = ax1.bar(years, bar_data, width=0.6, 15 color=_lighten(PALETTE[0], 0.4), edgecolor=PALETTE[0], 16 linewidth=1.2, label='数量', zorder=2) 17 18 # 柱子阴影 19 ax1.bar(years + 0.03, bar_data, width=0.6, color='#cccccc', alpha=0.08, zorder=1) 20 21 # 渐变填充折线 22 line = ax2.plot(years, line_data, 'o-', color=PALETTE[1], linewidth=2.2, markersize=7, 23 markeredgecolor='white', markeredgewidth=1.2, label='增长率', zorder=5) 24 for layer, alpha in enumerate([0.15, 0.08, 0.03]): 25 ax2.fill_between(years, min(line_data) - 2 + layer * 0.5, 26 np.array(line_data) - layer * 0.5, 27 alpha=alpha, color=PALETTE[1], linewidth=0) 28 29 # 峰值高亮 30 peak_idx = np.argmax(line_data) 31 ax2.scatter(years[peak_idx], line_data[peak_idx], s=150, color=PALETTE[1], 32 edgecolor='white', linewidth=2.5, zorder=6) 33 ax2.annotate(f'峰值 {line_data[peak_idx]:.1f}%', 34 xy=(years[peak_idx], line_data[peak_idx]), 35 xytext=(years[peak_idx] - 2, line_data[peak_idx] + 3), 36 fontsize=9, fontweight='bold', color=PALETTE[1], 37 arrowprops=dict(arrowstyle='->', color=PALETTE[1], lw=1.2), 38 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 39 edgecolor=PALETTE[1], alpha=0.9)) 40 41 # 相关性标注框 42 corr = np.corrcoef(bar_data, line_data)[0, 1] 43 ax1.text(0.02, 0.95, f'r = {corr:.3f}', 44 transform=ax1.transAxes, fontsize=9, va='top', 45 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 46 edgecolor=COLORS['grid'], alpha=0.9, linewidth=0.8)) 47 48 ax1.set_xlabel('年份', fontsize=11) 49 ax1.set_ylabel('数量 (个)', fontsize=11, color=PALETTE[0]) 50 ax2.set_ylabel('增长率 (%)', fontsize=11, color=PALETTE[1]) 51 ax1.tick_params(axis='y', labelcolor=PALETTE[0]) 52 ax2.tick_params(axis='y', labelcolor=PALETTE[1]) 53 54 # 合并图例 55 lines1, labels1 = ax1.get_legend_handles_labels() 56 lines2, labels2 = ax2.get_legend_handles_labels() 57 ax1.legend(lines1 + lines2, labels1 + labels2, frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9, loc='upper left') 58 59 ax1.spines['top'].set_visible(False) 60 ax1.grid(axis='y', alpha=0.12, linestyle='--', color=COLORS['grid']) 61 fig.tight_layout() 62 save_fig(fig, 'figures/fig_dual_axis.pdf')