代码如下:
# coding: utf-8
import matplotlib.pyplot as plt
precisions = list()
recalls = list()
f1s = list()
# TODO 将**替换为你的文件路径
filepath = "**"
# 每行格式为x值、key值、value值,\t分割
for line in open(filepath):
segs = line.strip().split("\t")
if len(segs) < 3:
continue
[x, key, value] = segs
x = int(x)
value = float(value)
if key == "precision":
precisions.append([x, value])
elif key == "recall":
recalls.append([x, value])
elif key == "f1":
f1s.append([x, value])
# 横纵轴的内容
plt.xlabel('iter')
plt.ylabel('value')
precisions = sorted(precisions)
plt.plot([x[0] for x in precisions], [x[1] for x in precisions], 'r', label='precision')
recalls = sorted(recalls)
plt.plot([x[0] for x in recalls], [x[1] for x in recalls], 'g', label='recall')
f1s = sorted(f1s)
plt.plot([x[0] for x in f1s], [x[1] for x in f1s], 'b', label='f1')
plt.legend()
plt.grid()
plt.show()效果如下:
