python绘制散点图

import matplotlib.pyplot as plt

# 数据
methods = ["A", "B", "C", "D", "E", "F"]
iou = [1, 2, 3, 4, 5, 6]
recall = [1, 2, 3, 4, 5, 6]
precision = [1, 2, 3, 4, 5, 6]

plt.style.use('seaborn')
# 创建一个包含三个子图的图
fig, axs = plt.subplots(1, 3, figsize=(15, 5))

# 定义新颖的标记形状
'''
'H' - 六边形;'P' - 五边形;'X' - X 形状;'s' - 正方形;
'D' - 菱形;'^' - 上三角形;'v' - 下三角形;'<' - 左三角形;'>' - 右三角形''
'''
marker_shapes = ['o', 's', 'D', '^', 'v', '<']

# 子图1:IoU vs. 方法
axs[0].scatter(methods, iou, label="IoU", marker='H', color='#1f77b4', s=150, edgecolors='k', linewidth=2)
axs[0].set_title("IoU Comparison")
axs[0].set_ylabel('IoU (%)')
axs[0].set_ylim(0, 100)

# 添加数据标签
for i, txt in enumerate(iou):
    axs[0].annotate(f'{txt:.2f}', (methods[i], iou[i]), textcoords="offset points", xytext=(0, 10), ha='center')

# 子图2:Recall vs. 方法
axs[1].scatter(methods, recall, label="Recall", marker='P', color='#ff7f0e', s=150, edgecolors='k', linewidth=2)
axs[1].set_title("Recall Comparison")
axs[1].set_ylabel('Recall (%)')
axs[1].set_ylim(0, 100)

# 添加数据标签
for i, txt in enumerate(recall):
    axs[1].annotate(f'{txt:.2f}', (methods[i], recall[i]), textcoords="offset points", xytext=(0, 10), ha='center')

# 子图3:Precision vs. 方法
axs[2].scatter(methods, precision, label="Precision", marker='D', color='#2ca02c', s=150, edgecolors='k', linewidth=2)
axs[2].set_title("Precision Comparison")
axs[2].set_ylabel('Precision (%)')
axs[2].set_ylim(0, 100)

# 添加数据标签
for i, txt in enumerate(precision):
    axs[2].annotate(f'{txt:.2f}', (methods[i], precision[i]), textcoords="offset points", xytext=(0, 10), ha='center')

# 调整子图之间的间距
plt.tight_layout()
# 添加背景颜色
fig.patch.set_facecolor('#f2f2f2')
# 添加图例
for ax in axs:
    ax.grid(axis='y', linestyle='--', alpha=0.7)
    ax.grid(visible=True, which='major', linestyle='-')
    ax.grid(visible=True, which='minor', linestyle='--', alpha=0.5)
    ax.legend()

# 显示图例
axs[0].legend()
# 自动调整子图布局
plt.tight_layout()

# 保存图像文件
plt.savefig('A.png', dpi=600, bbox_inches='tight')

# 显示图
plt.show()

你可能感兴趣的:(工具代码,python,matplotlib)