感谢一下悦姐帮忙
import re
#先把文件读进来,用read读入的是字符串,readlines是list
with open('resok.txt') as f:
txt = f.read()
dataset = r'5low:.*|5mix:.*|5normal:.*'
para = r'MAE: (.{6})'#意思是MAE: 后面的六个东西
row_data = re.findall(dataset,txt)#正则化找到要读的内容,先找到基本的行(dataset),在找到行里的内容(para)
new_row_data = '\n'.join(row_data)#list转化为字符串
final_data = re.findall(para,new_row_data)
# print(dataset)
# print('\n')
# print(new_row_data)
# print('\n')
# print(final_data)
print(len(final_data))#检查一下个数看对不对
然后画图,其实两个代码可以放在一个里面
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from readdata import final_data
#要画的标签以及每个标签对应的值
labels = [ 'CONet', 'DANet','DCF', 'SP-Net', 'VST', 'CDINet','DFM-Net','TriTransNet','cmSalGAN','D3Net','BiANet','DLMNet','CCAFNet','C2DFNet','MobileSal','JL-DCF','SwinNet','A2S-v2','HRTransNet']
Overall = []
low = []
normal = []
#print(final_data)
#把之前读进来的数据分给上面的对应的值
for i in range(len(final_data)):
final_data[i]=float(final_data[i])
if i % 3 == 0:
low.append(final_data[i])
if i % 3 == 1:
Overall.append(final_data[i])
if i % 3 == 2:
normal.append(final_data[i])
# print(Large)
# print('\n')
# print(Middle)
# print('\n')
# print(Overall)
# print('\n')
# print(Small)
# print('\n')
x = np.arange(len(labels)) # 标签位置
width = 0.2 # 柱状图的宽度,可以根据自己的需求和审美来改
fig, ax = plt.subplots()
rects1 = ax.bar(x - width, low, width, label='Low-light scenes',color=(33/255,158/255,188/255))
rects2 = ax.bar(x + 0.00, normal, width, label='Sunny scenes',color=(19/255,103/255,131/255))
rects3 = ax.bar(x + width, Overall, width, label='Overall',color=(2/255,48/255,74/255))
#为y轴、标题和x轴等添加一些文本。
# ax.set_ylabel(r'$F^{\omega}_{\beta}$', fontsize=12)
# ax.set_xlabel('Models', fontsize=12)
# ax.set_title('Result of 'r'$F^{\omega}_{\beta}$')
# ax.set_xticks(x)
# ax.set_xticklabels(labels,rotation=45)
# ax.set_ylim(0.4,1.1)
# ax.legend(loc='upper right')
ax.set_ylabel('MAE', fontsize=12)
ax.set_xlabel('Models', fontsize=12)
ax.set_title('Result of MAE')
ax.set_xticks(x)
ax.set_xticklabels(labels,rotation=45)
#可以自己控制
#轴的显示范围
#ax.set_ylim(0,0.24)
ax.legend(loc='upper right')
# def autolabel(rects):
# """在*rects*中的每个柱状条上方附加一个文本标签,显示其高度"""
# for rect in rects:
# height = rect.get_height()
# ax.annotate('{}'.format(height),
# xy=(rect.get_x() + rect.get_width() / 2, height),
# xytext=(0, 3), # 3点垂直偏移
# textcoords="offset points",
# ha='center', va='bottom')
# autolabel(rects1)
# autolabel(rects2)
# autolabel(rects3)
# autolabel(rects4)
#fig.tight_layout()
#plt.xticks(labels, labels, rotation=30)
plt.show()
最后贴个图: