import matplotlib.pyplot as plt
import os
import numpy as np
# 路径
path = input("请输入文件路径:")
# 文件列表 acc
files_txt = []
for file_t in os.listdir(path):
if file_t.endswith(".txt"):
files_txt.append(file_t)
print (files_txt)
'''
#文件列表 loss
files_AlexNet_loss = []
for file_loss in os.listdir(path):
if file_loss.endswith("AlexNet_loss.txt"):
files_AlexNet_loss.append(path + file_loss)
'''
# 遍历所有文件 acc
def loadData(file,i):
files = open(file)
#title1=file.readlines()[0]
#print(title1)
#----------- 逐行读取文件内的数据 ------------
data = files.readlines()[3176+i*61:3237+i*61]
#----------- 根据自己的需要查看data的内容 ---------
#print(data)
#filename='Block_0x000A_Page_0x0000'
'''
txt文件的数值为y轴的数据
所以x要根据y的个数有序生成
'''
#------ x轴数据有序生成150个(根据自己的横坐标范围自己修改范围) ----
#---------- 新建一个空的列表,用于存储上一步逐行读取的data ------------
y = []
#---------- 用循环的方式添加进列表 -----------
for num in data:
#------split用于将每一行数据用逗号分割成多个对象-----
#------取分割后的第0列,转换成float格式后添加到列表中-------
y.append(float(num.split(',')[0]))
return y
x = np.arange(0,61)
#画图
def plotData(x,y):
pass #这个日后优化
for i in range(0,25):
if __name__ == '__main__':
#此处是画出acc的图,要画出loss的图,只需把files_AlexNet_acc中的acc换成loss即可
y = loadData(files_txt[0],i)
plt.plot(x, y, color='r', alpha=0.6, label='1')
plt.legend()
y = loadData(files_txt[1],i)
plt.plot(x, y, color='g', alpha=0.6, label='2')
plt.legend()
y = loadData(files_txt[2],i)
plt.plot(x, y, color='b', alpha=0.6, label='3')
plt.legend()
y = loadData(files_txt[3],i)
plt.plot(x, y, color='y', alpha=0.6, label='4')
plt.legend()
y = loadData(files_txt[4],i)
plt.plot(x, y, color='c', alpha=0.6, label='5')
plt.legend()
y = loadData(files_txt[5],i)
plt.plot(x, y, color='m', alpha=0.6, label='6')
plt.legend()
y = loadData(files_txt[6],i)
plt.plot(x, y, color='salmon', alpha=0.6, label='7')
plt.legend()
y = loadData(files_txt[7],i)
plt.plot(x, y, color='k', alpha=0.6, label='8')
plt.legend()
y = loadData(files_txt[8],i)
plt.plot(x, y, color='teal', alpha=0.6, label='9')
plt.legend()
y = loadData(files_txt[9],i)
plt.plot(x, y, color='violet', alpha=0.6, label='10')
plt.legend()
y = loadData(files_txt[10],i)
plt.plot(x, y, color='black', alpha=0.6, label='11')
plt.legend()
y = loadData(files_txt[11],i)
plt.plot(x, y, color='dimgray', alpha=0.6, label='12')
plt.legend()
y = loadData(files_txt[12],i)
plt.plot(x, y, color='lightcoral', alpha=0.6, label='13')
plt.legend()
y = loadData(files_txt[13],i)
plt.plot(x, y, color='chocolate', alpha=0.6, label='14')
plt.legend()
y = loadData(files_txt[14],i)
plt.plot(x, y, color='tan', alpha=0.6, label='15')
plt.legend()
y = loadData(files_txt[15],i)
plt.plot(x, y, color='orange', alpha=0.6, label='16')
plt.legend()
y = loadData(files_txt[16],i)
plt.plot(x, y, color='gold', alpha=0.6, label='17')
plt.legend()
y = loadData(files_txt[17],i)
plt.plot(x, y, color='yellow', alpha=0.6, label='18')
plt.legend()
y = loadData(files_txt[18],i)
plt.plot(x, y, color='greenyellow', alpha=0.6, label='19')
plt.legend()
y = loadData(files_txt[19],i)
plt.plot(x, y, color='coral', alpha=0.6, label='20')
plt.legend()
y = loadData(files_txt[20],i)
plt.plot(x, y, color='palegreen', alpha=0.6, label='21')
plt.legend()
y = loadData(files_txt[21],i)
plt.plot(x, y, color='seagreen', alpha=0.6, label='22')
plt.legend()
y = loadData(files_txt[22],i)
plt.plot(x, y, color='lightseagreen', alpha=0.6, label='23')
plt.legend()
y = loadData(files_txt[23],i)
plt.plot(x, y, color='teal', alpha=0.6, label='24')
plt.legend()
y = loadData(files_txt[24],i)
plt.plot(x, y, color='lime', alpha=0.6, label='25')
plt.legend()
y = loadData(files_txt[25],i)
plt.plot(x, y, color='cyan', alpha=0.6, label='26')
plt.legend()
y = loadData(files_txt[26],i)
plt.plot(x, y, color='skyblue', alpha=0.6, label='27')
plt.legend()
y = loadData(files_txt[27],i)
plt.plot(x, y, color='plum', alpha=0.6, label='28')
plt.legend()
y = loadData(files_txt[28],i)
plt.plot(x, y, color='violet', alpha=0.6, label='29')
plt.legend()
y = loadData(files_txt[29],i)
plt.plot(x, y, color='grey', alpha=0.6, label='30')
plt.legend()
plt.title("test plot")
plt.xlabel("iteration")
plt.ylabel("accuracy")
#plt.xticks([5000, 10000, 15000], ['5000', '10000', '15000'])
target_path=input("请输入图片保存路径:")
pic_name=input("请给图片命名:")
plt.savefig(target_path+'\\'+pic_name)
plt.show()
这是一个将多个txt文档3177行之后的数据利用plot插件画图的功能,改了后面图片名和地址的设定,其他都一样。
import matplotlib.pyplot as plt
import os
import numpy as np
# 路径
path = input("请输入文件路径:")
# 文件列表 acc
files_txt = []
for file_t in os.listdir(path):
if file_t.endswith(".txt"):
files_txt.append(file_t)
print (files_txt)
'''
#文件列表 loss
files_AlexNet_loss = []
for file_loss in os.listdir(path):
if file_loss.endswith("AlexNet_loss.txt"):
files_AlexNet_loss.append(path + file_loss)
'''
# 遍历所有文件 acc
def loadData(file,i):
files = open(file)
#title1=file.readlines()[0]
#print(title1)
#----------- 逐行读取文件内的数据 ------------
data = files.readlines()[3176+i*61:3237+i*61]
#----------- 根据自己的需要查看data的内容 ---------
#print(data)
#filename='Block_0x000A_Page_0x0000'
'''
txt文件的数值为y轴的数据
所以x要根据y的个数有序生成
'''
#------ x轴数据有序生成150个(根据自己的横坐标范围自己修改范围) ----
#---------- 新建一个空的列表,用于存储上一步逐行读取的data ------------
y = []
#---------- 用循环的方式添加进列表 -----------
for num in data:
#------split用于将每一行数据用逗号分割成多个对象-----
#------取分割后的第0列,转换成float格式后添加到列表中-------
y.append(float(num.split(',')[0]))
return y
target_path=input("请输入图片保存路径:")
x = np.arange(0,61)
#画图
def plotData(x,y):
pass #这个日后优化
for i in range(25):
if __name__ == '__main__':
plt.figure(figsize=(15, 6.5))
#此处是画出acc的图,要画出loss的图,只需把files_AlexNet_acc中的acc换成loss即可
y = loadData(files_txt[0],i)
plt.plot(x, y, color='r', alpha=0.6, label='1')
#plt.legend()
#plt.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.8, handleheight=1.675)
#plt.subplots_adjust(right=0.7)
#plt.figure(figsize=(15, 6.5))
y = loadData(files_txt[1],i)
plt.plot(x, y, color='g', alpha=0.6, label='2')
#plt.legend()
y = loadData(files_txt[2],i)
plt.plot(x, y, color='b', alpha=0.6, label='3')
plt.legend()
y = loadData(files_txt[3],i)
plt.plot(x, y, color='y', alpha=0.6, label='4')
plt.legend()
y = loadData(files_txt[4],i)
plt.plot(x, y, color='c', alpha=0.6, label='5')
plt.legend()
y = loadData(files_txt[5],i)
plt.plot(x, y, color='m', alpha=0.6, label='6')
plt.legend()
y = loadData(files_txt[6],i)
plt.plot(x, y, color='salmon', alpha=0.6, label='7')
plt.legend()
y = loadData(files_txt[7],i)
plt.plot(x, y, color='k', alpha=0.6, label='8')
plt.legend()
y = loadData(files_txt[8],i)
plt.plot(x, y, color='teal', alpha=0.6, label='9')
plt.legend()
y = loadData(files_txt[9],i)
plt.plot(x, y, color='violet', alpha=0.6, label='10')
plt.legend()
y = loadData(files_txt[10],i)
plt.plot(x, y, color='black', alpha=0.6, label='11')
plt.legend()
y = loadData(files_txt[11],i)
plt.plot(x, y, color='dimgray', alpha=0.6, label='12')
plt.legend()
y = loadData(files_txt[12],i)
plt.plot(x, y, color='lightcoral', alpha=0.6, label='13')
plt.legend()
y = loadData(files_txt[13],i)
plt.plot(x, y, color='chocolate', alpha=0.6, label='14')
plt.legend()
y = loadData(files_txt[14],i)
plt.plot(x, y, color='tan', alpha=0.6, label='15')
plt.legend()
y = loadData(files_txt[15],i)
plt.plot(x, y, color='orange', alpha=0.6, label='16')
plt.legend()
y = loadData(files_txt[16],i)
plt.plot(x, y, color='gold', alpha=0.6, label='17')
plt.legend()
y = loadData(files_txt[17],i)
plt.plot(x, y, color='yellow', alpha=0.6, label='18')
plt.legend()
y = loadData(files_txt[18],i)
plt.plot(x, y, color='greenyellow', alpha=0.6, label='19')
plt.legend()
y = loadData(files_txt[19],i)
plt.plot(x, y, color='coral', alpha=0.6, label='20')
plt.legend()
y = loadData(files_txt[20],i)
plt.plot(x, y, color='palegreen', alpha=0.6, label='21')
plt.legend()
y = loadData(files_txt[21],i)
plt.plot(x, y, color='seagreen', alpha=0.6, label='22')
plt.legend()
y = loadData(files_txt[22],i)
plt.plot(x, y, color='lightseagreen', alpha=0.6, label='23')
plt.legend()
y = loadData(files_txt[23],i)
plt.plot(x, y, color='teal', alpha=0.6, label='24')
plt.legend()
y = loadData(files_txt[24],i)
plt.plot(x, y, color='lime', alpha=0.6, label='25')
plt.legend()
y = loadData(files_txt[25],i)
plt.plot(x, y, color='cyan', alpha=0.6, label='26')
plt.legend()
y = loadData(files_txt[26],i)
plt.plot(x, y, color='skyblue', alpha=0.6, label='27')
plt.legend()
y = loadData(files_txt[27],i)
plt.plot(x, y, color='plum', alpha=0.6, label='28')
plt.legend()
y = loadData(files_txt[28],i)
plt.plot(x, y, color='violet', alpha=0.6, label='29')
plt.legend()
y = loadData(files_txt[29],i)
plt.plot(x, y, color='grey', alpha=0.6, label='30')
plt.legend()
plt.title("test plot")
plt.xlabel("iteration")
plt.ylabel("accuracy")
#plt.savefig(str(i))
#plt.xticks([5000, 10000, 15000], ['5000', '10000', '15000'])
#target_path=input("请输入图片保存路径:")
#pic_name=input("请给图片命名:")
plt.get_current_fig_manager().window.state('zoomed')
#plt.figure(figsize=(15, 6.5))
plt.savefig(target_path+'\\'+str(i),dpi=200, bbox_inches='tight')
plt.show()