import matplotlib.pyplot as plt
# 示例数据
algorithms = ['Algorithm A', 'Algorithm B', 'Algorithm C', 'Algorithm D']
compute = [1.2e9, 2.5e9, 3.0e9, 0.8e9] # 计算量(FLOPs)
map_scores = [0.75, 0.82, 0.68, 0.90] # mAP值
params = [5e6, 12e6, 8e6, 3e6] # 参数量
# 数据验证
if len(algorithms) != len(compute) or len(algorithms) != len(map_scores) or len(algorithms) != len(params):
raise ValueError("所有输入列表的长度必须一致。")
# 调整参数量的显示大小
scaled_params = [p / 1e4 for p in params]
# 创建图表
plt.figure(figsize=(12, 8), dpi=100)
colors = plt.cm.tab10.colors[:len(algorithms)]
# 绘制气泡图
scatter = plt.scatter(
x=compute,
y=map_scores,
s=scaled_params,
c=colors,
alpha=0.7,
edgecolors='w',
linewidths=1.5
)
# 添加算法名称标签
for i, (x, y) in enumerate(zip(compute, map_scores)):
plt.annotate(
algorithms[i],
(x, y),
xytext=(5, 5),
textcoords='offset points',
ha='center',
fontsize=9
)
# 设置坐标轴格式
plt.gca().xaxis.set_major_formatter(lambda x, _: f'{x / 1e9:.1f}G')
plt.xlabel('Computational Load (GFLOPs)')
plt.ylabel('mAP Score')
plt.title('Algorithm Comparison\n(Bubble Size = Parameters)')
plt.grid(True, linestyle='--', alpha=0.6)
# 创建算法图例
algo_legend = plt.legend(
handles=[
plt.scatter([], [], s=50, color=color, label=algo, alpha=0.7)
for algo, color in zip(algorithms, colors)
],
title='Algorithms',
loc='upper left',
bbox_to_anchor=(1.05, 1)
)
# 创建参数量图例
param_legend = plt.legend(
handles=[
plt.scatter([], [], s=s / 1e4, color='gray', alpha=0.7, label=f'{int(s / 1e6)}M')
for s in [3e6, 6e6, 9e6, 12e6] # 示例参数值
],
title='Parameters',
loc='upper left',
bbox_to_anchor=(1.05, 0.7)
)
# 将算法图例和参数量图例添加到图表
plt.gca().add_artist(algo_legend)
plt.gca().add_artist(param_legend)
# 保存图片
plt.savefig('algorithm_comparison.png', bbox_inches='tight')
plt.tight_layout()
plt.show()