1 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten, smart_labels 2 setup_style() 3 import matplotlib.pyplot as plt 4 import matplotlib.colors as mc 5 import numpy as np 6 import colorsys 7 8 methods = ['Ours', 'Baseline-A', 'Baseline-B', 'Baseline-C', 'Baseline-D'] 9 scores = [0.923, 0.887, 0.862, 0.841, 0.815] 10 11 n = len(methods) 12 score_min, score_max = min(scores), max(scores) 13 score_range = score_max - score_min if score_max > score_min else 1 14 15 # ── 渐变配色:从紫罗兰 → 珊瑚橙(明度和色相同时渐变) 16 color_top = '#7B6BA5' # 紫罗兰(高分,偏冷一点) 17 color_bottom = '#E08B74' # 珊瑚橙(低分,偏暖) 18 19 def interpolate_color(c1, c2, t): 20 """HSL 空间插值:t=0 返回 c1,t=1 返回 c2""" 21 r1, g1, b1 = mc.to_rgb(c1) 22 r2, g2, b2 = mc.to_rgb(c2) 23 h1, l1, s1 = colorsys.rgb_to_hls(r1, g1, b1) 24 h2, l2, s2 = colorsys.rgb_to_hls(r2, g2, b2) 25 if abs(h2 - h1) > 0.5: 26 if h1 < h2: h1 += 1.0 27 else: h2 += 1.0 28 h = (h1 + (h2 - h1) * t) % 1.0 29 l = l1 + (l2 - l1) * t 30 s = s1 + (s2 - s1) * t 31 return colorsys.hls_to_rgb(h, l, s) 32 33 item_colors = [interpolate_color(color_top, color_bottom, i / (n - 1) if n > 1 else 0) for i in range(n)] 34 35 # ── 自适应高度(每项 0.46 高度 + 上下留白) 36 _fig_h = max(4, n * 0.46 + 1.8) 37 fig, ax = plt.subplots(figsize=(7.5, _fig_h)) 38 y_pos = np.arange(n) 39 40 # 极浅网格线 41 ax.grid(axis='x', alpha=0.12, linestyle='-', color=COLORS['grid']) 42 ax.set_axisbelow(True) 43 44 # 中位数参考线(置于底层) 45 median_val = np.median(scores) 46 ax.axvline(median_val, color=COLORS['ref_line'], linestyle=':', linewidth=1.0, alpha=0.5, zorder=1) 47 48 # ── 主体:渐变色茎线 + 渐变圆点 49 for i, (m, s) in enumerate(zip(methods, scores)): 50 c = item_colors[i] 51 ratio = (s - score_min) / score_range 52 lw = 1.6 + 2.0 * ratio 53 54 # 茎线从 0 开始 55 ax.plot([0, s], [y_pos[i], y_pos[i]], 56 color=c, linewidth=lw, zorder=3, solid_capstyle='round') 57 58 # 端点圆点 —— 大小随分数渐变 59 dot_size = 55 + 120 * ratio 60 ax.scatter(s, y_pos[i], color=c, s=dot_size, zorder=5, 61 edgecolors='white', linewidths=1.8) 62 63 # 数值标签 64 ax.text(s + score_range * 0.03, y_pos[i], f'{s:.3f}', 65 fontsize=8.5, fontweight='bold' if i < 3 else 'normal', 66 color=c, va='center', ha='left') 67 68 # ── 排名徽章区域 69 badge_x = -score_range * 0.065 70 rank = i + 1 71 if rank <= 3: 72 badge = plt.Circle((badge_x, y_pos[i]), 0.3, 73 color=_lighten(c, 0.15), zorder=6, 74 transform=ax.transData) 75 ax.add_patch(badge) 76 ax.text(badge_x, y_pos[i], str(rank), 77 fontsize=8.5, fontweight='bold', color='white', 78 ha='center', va='center', zorder=7) 79 else: 80 ax.text(badge_x, y_pos[i], str(rank), 81 fontsize=7.5, color=_lighten(c, 0.2), 82 ha='center', va='center', fontweight='bold') 83 84 # 第一名背景高亮条 85 ax.axhspan(y_pos[0] - 0.42, y_pos[0] + 0.42, alpha=0.06, 86 color=item_colors[0], zorder=0) 87 88 # 中位数标注 —— 置于图的顶部 89 ax.text(median_val, -0.9, f'中位数 {median_val:.3f}', 90 fontsize=8, color=COLORS['ref_line'], ha='center', va='bottom', 91 bbox=dict(boxstyle='round,pad=0.25', facecolor='white', 92 edgecolor=COLORS['ref_line'], alpha=0.85)) 93 94 ax.set_yticks(y_pos) 95 ax.set_yticklabels(methods, fontsize=10) 96 ax.set_xlabel('F1 Score', fontsize=11) 97 ax.set_xlim(-score_range * 0.13, score_max + score_range * 0.15) 98 ax.set_ylim(n - 0.5, -1.4) 99 ax.spines['top'].set_visible(False) 100 ax.spines['right'].set_visible(False) 101 fig.tight_layout() 102 save_fig(fig, 'figures/fig_lollipop.pdf')