import matplotlib.pyplot as plt import numpy as np # 数据 categories = ['DINO-B/8', 'DINO-B/16', 'SAM-B/16', 'SAM-L/16', 'DINOv2-B/14', 'DINOv2-L/14'] x = np.arange(len(categories)) - 0.6 # 整体向左平移,使得第一组更靠近 y 轴 # 每个分组的结果数据(去掉 CityScape) VOC21 = [59.7, 61.5, 55.0, 58.9, 64.1, 62.8] COCO_Obj = [31.2, 32.3, 25.8, 30.7, 38.7, 36.7] OV_COCO = [42.0, 41.8, 42.6, 42.5, 43.3, 43.3] # 设置柱状图宽度 width = 0.25 # 调整宽度以适应更少的类别 # 创建图形 fig, ax = plt.subplots(figsize=(8, 4)) # 调整图形大小 # 绘制柱状图,调整颜色和填充 bars1 = ax.bar(x - width, VOC21, width, label=r'VOC21', color='#1f77b4', edgecolor='white', hatch='X') # 第1组 bars2 = ax.bar(x, COCO_Obj, width, label=r'COCO-Obj', color="#ff7f0e", edgecolor='white', hatch='X') # 第2组 bars3 = ax.bar(x + width, OV_COCO, width, label=r'OV-COCO', color='#2ca02c', edgecolor='white', hatch='X') # 第3组 # 添加标题和标签 ax.set_xticks(x) ax.set_xticklabels(categories, fontsize=16, ha='center') # 调整字体大小并居中显示 # 自动放置 legend,略微调大方块显示 ax.legend(fontsize=14, loc='upper center', bbox_to_anchor=(0.46, 1.0), ncol=3, frameon=True, handletextpad=0.5, columnspacing=1.0, handlelength=1.5, handleheight=1.3) # handlelength 和 handleheight 稍微调大 # 在柱状图上添加数值,并标注最大值 highlight_fontsize = 15 # 可手动调整红色加粗文本的字体大小 for bar, value in zip(bars1, VOC21): if value == max(VOC21): ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{value:.1f}', ha='center', va='bottom', fontsize=highlight_fontsize, color='red', fontweight='bold') else: ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{value:.1f}', ha='center', va='bottom', fontsize=14) for bar, value in zip(bars2, COCO_Obj): if value == max(COCO_Obj): ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{value:.1f}', ha='center', va='bottom', fontsize=highlight_fontsize, color='red', fontweight='bold') else: ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{value:.1f}', ha='center', va='bottom', fontsize=14) for bar, value in zip(bars3, OV_COCO): if value == max(OV_COCO): # 两个最大值都加粗 ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{value:.1f}', ha='center', va='bottom', fontsize=highlight_fontsize, color='red', fontweight='bold') else: ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{value:.1f}', ha='center', va='bottom', fontsize=14) # 美化图形 ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) # 保存为图片 plt.tight_layout() plt.savefig('vfm_ablation.png', dpi=300)