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 categories = ['Accuracy', 'Precision', 'Recall', 'F1', 'Speed', 'Memory'] 7 N = len(categories) 8 angles = np.linspace(0, 2 * np.pi, N, endpoint=False).tolist() + [0] 9 10 methods = { 11 'Ours': [0.95, 0.93, 0.91, 0.92, 0.70, 0.65], 12 'Transformer': [0.92, 0.90, 0.88, 0.89, 0.50, 0.40], 13 'CNN': [0.88, 0.85, 0.90, 0.87, 0.85, 0.80], 14 } 15 16 def polygon_area(vals, angles_list): 17 """Calculate polygon area for radar chart""" 18 n = len(vals) 19 area = 0 20 for i in range(n): 21 j = (i + 1) % n 22 area += vals[i] * vals[j] * np.sin(angles_list[j] - angles_list[i]) 23 return abs(area) / 2 24 25 fig, ax = plt.subplots(figsize=(6.5, 6), subplot_kw=dict(polar=True)) 26 27 # Threshold ring at 0.8 28 threshold = 0.8 29 ax.plot(angles, [threshold] * (N + 1), '--', color=COLORS['ref_line'], linewidth=1, alpha=0.4) 30 ax.text(0.02, 0.02, f'Baseline ({threshold})', fontsize=7, color=COLORS['ref_line'], 31 transform=ax.transAxes, ha='left', va='bottom') 32 33 areas = {} 34 for i, (name, vals) in enumerate(methods.items()): 35 vc = vals + vals[:1] 36 lw = 2.5 if name == 'Ours' else 1.5 37 alpha_fill = 0.18 if name == 'Ours' else 0.06 38 ax.plot(angles, vc, 'o-', linewidth=lw, markersize=5, color=PALETTE[i], label=name, 39 markeredgecolor='white', markeredgewidth=0.8) 40 ax.fill(angles, vc, alpha=alpha_fill, color=PALETTE[i]) 41 42 # Value labels at each vertex — only label "Ours" to avoid overlap 43 if name == 'Ours': 44 for j, (ang, val) in enumerate(zip(angles[:-1], vals)): 45 offset_r = 0.08 46 ax.text(ang, val + offset_r, f'{val:.2f}', fontsize=7, ha='center', va='bottom', 47 color=PALETTE[i], fontweight='bold', 48 bbox=dict(boxstyle='round,pad=0.15', facecolor='white', 49 edgecolor=PALETTE[i], alpha=0.85, linewidth=0.4)) 50 51 areas[name] = polygon_area(vals, angles[:-1]) 52 53 # Area ratio annotation in center 54 ours_area = areas['Ours'] 55 ratios = [f'{name}: {ours_area / a:.2f}×' for name, a in areas.items() if name != 'Ours'] 56 ax.text(0, 0, f'Area ratio\nvs Ours:\n' + '\n'.join(ratios), 57 ha='center', va='center', fontsize=7, color=COLORS['text'], 58 transform=ax.transData, 59 bbox=dict(boxstyle='round,pad=0.4', facecolor='white', 60 edgecolor=COLORS['grid'], alpha=0.9)) 61 62 ax.set_xticks(angles[:-1]) 63 ax.set_xticklabels(categories, fontsize=9) 64 ax.set_ylim(0, 1.05) 65 ax.set_yticks([0.2, 0.4, 0.6, 0.8, 1.0]) 66 ax.set_yticklabels(['', '0.4', '0.6', '0.8', '1.0'], fontsize=7, color=COLORS['ref_line']) 67 ax.legend(loc='best', bbox_to_anchor=(1.35, 1.1), frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9) 68 fig.tight_layout() 69 save_fig(fig, 'figures/fig_radar.pdf')