import matplotlib.pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:windowsfontsSimSun.ttc", size=16)
def ReadTxtName(rootdir):
lines = []
with open(rootdir, 'r') as file_to_read:
while True:
line = file_to_read.readline()
if not line:
break
line = line.strip('n')
lines.append(float(line))
return lines
resultpath = r'C:Users411Desktoplosspspnetloss.txt' #四个网络loss值
resultpath2 = r'C:Users411Desktoplosssegnetloss.txt'
resultpath3 = r'C:Users411Desktoplossunetloss.txt'
resultpath4= r'C:Users411Desktoploss修改loss.txt'
lineslist = ReadTxtName(resultpath)
lineslist2 = ReadTxtName(resultpath2)
lineslist3 = ReadTxtName(resultpath3)
lineslist4 = ReadTxtName(resultpath4)
x=range(0,100) #100
y1=lineslist
y2=lineslist2
y3=lineslist3
y4=lineslist4
plt.title('Result Analysis',fontproperties=font)
plt.plot(x, y1,color='black', label='Net1') #四条曲线
plt.plot(x, y2, color='blue', label='Net2')
plt.plot(x, y3,color='red',label='Net3')
plt.plot(x, y4, color='green',label='improveNet')
plt.legend(prop=font) # 显示图例
plt.xlabel('Epoch',fontproperties=font)
plt.ylabel('Loss',fontproperties=font)
plt.show()
结果图



