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 np.random.seed(42) 7 categories = ['Dataset A', 'Dataset B', 'Dataset C', 'Dataset D'] 8 group_names = ['Ours', 'Baseline-1', 'Baseline-2'] 9 n_groups = len(group_names) 10 11 # Generate sample data (replace with real results) 12 all_data = {} 13 for g, gname in enumerate(group_names): 14 all_data[gname] = [] 15 for c in range(len(categories)): 16 center = 80 + g * (-3) + c * 2 + np.random.randn() 17 d = np.random.normal(center, 3 + g, 50) 18 all_data[gname].append(d) 19 20 fig, ax = plt.subplots(figsize=(9, 5)) 21 width = 0.25 22 positions_base = np.arange(len(categories)) 23 24 for g, gname in enumerate(group_names): 25 positions = positions_base + (g - n_groups / 2 + 0.5) * width 26 color = PALETTE[g % len(PALETTE)] 27 light = _lighten(color, 0.4) 28 29 parts = ax.violinplot(all_data[gname], positions=positions, 30 widths=width * 0.85, showmeans=False, 31 showmedians=False, showextrema=False) 32 33 for pc in parts['bodies']: 34 pc.set_facecolor(light) 35 pc.set_edgecolor(color) 36 pc.set_linewidth(1.2) 37 pc.set_alpha(0.8) 38 39 # Add median + quartile lines manually 40 for i, d in enumerate(all_data[gname]): 41 q1, med, q3 = np.percentile(d, [25, 50, 75]) 42 pos = positions[i] 43 # Median dot 44 ax.scatter(pos, med, color=color, s=30, zorder=5, 45 edgecolors='white', linewidths=0.8) 46 # Quartile whisker 47 ax.vlines(pos, q1, q3, color=color, linewidth=2.5, zorder=4) 48 49 # Invisible scatter for legend 50 ax.scatter([], [], color=color, s=60, label=gname, edgecolors='white') 51 52 ax.set_xticks(positions_base) 53 ax.set_xticklabels(categories, fontsize=10) 54 ax.set_ylabel('Score', fontsize=11) 55 ax.legend(frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=9) 56 ax.spines['top'].set_visible(False) 57 ax.spines['right'].set_visible(False) 58 ax.grid(axis='y', alpha=0.1, linestyle='--') 59 fig.tight_layout() 60 save_fig(fig, 'figures/fig_grouped_violin.pdf')