这是一个利用plot将多个txt文档的7~3174行画图的功能

该代码段使用Python读取指定路径下的一系列.txt文件,处理数据并使用matplotlib进行可视化。它遍历文件夹,找到特定格式的文件,然后加载数据并绘制图形,每个文件对应图中的一个曲线。图形包含多个颜色区分的曲线,表示不同文件的数据,x轴为迭代次数,y轴为精度(accuracy)。
摘要由CSDN通过智能技术生成

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):
    
    files = open(file)

    #title1=file.readlines()[0]
    #print(title1)


    #-----------  逐行读取文件内的数据  ------------
    data = files.readlines()[6:3174]
    #-----------  根据自己的需要查看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,3168)
 
#画图
def plotData(x,y):
    pass   #这个日后优化
 
if __name__ == '__main__':

    plt.figure(figsize=(15, 7.5))
    #此处是画出acc的图,要画出loss的图,只需把files_AlexNet_acc中的acc换成loss即可
    y = loadData(files_txt[0])
    plt.plot(x, y, color='r', alpha=0.6, label='1')
    plt.legend()
 
    y = loadData(files_txt[1])
    plt.plot(x, y, color='g', alpha=0.6, label='2')
    plt.legend()
 
    y = loadData(files_txt[2])
    plt.plot(x, y, color='b', alpha=0.6, label='3')
    plt.legend()
 
    y = loadData(files_txt[3])
    plt.plot(x, y, color='y', alpha=0.6, label='4')
    plt.legend()
 
    y = loadData(files_txt[4])
    plt.plot(x, y, color='c', alpha=0.6, label='5')
    plt.legend()
 
    y = loadData(files_txt[5])
    plt.plot(x, y, color='m', alpha=0.6, label='6')
    plt.legend()

    y = loadData(files_txt[6])
    plt.plot(x, y, color='salmon', alpha=0.6, label='7')
    plt.legend()

    y = loadData(files_txt[7])
    plt.plot(x, y, color='k', alpha=0.6, label='8')
    plt.legend()
    
    y = loadData(files_txt[8])
    plt.plot(x, y, color='teal', alpha=0.6, label='9')
    plt.legend()

    y = loadData(files_txt[9])
    plt.plot(x, y, color='violet', alpha=0.6, label='10')
    plt.legend()

    y = loadData(files_txt[10])
    plt.plot(x, y, color='black', alpha=0.6, label='11')
    plt.legend()

    y = loadData(files_txt[11])
    plt.plot(x, y, color='dimgray', alpha=0.6, label='12')
    plt.legend()

    y = loadData(files_txt[12])
    plt.plot(x, y, color='lightcoral', alpha=0.6, label='13')
    plt.legend()

    y = loadData(files_txt[13])
    plt.plot(x, y, color='chocolate', alpha=0.6, label='14')
    plt.legend()

    y = loadData(files_txt[14])
    plt.plot(x, y, color='tan', alpha=0.6, label='15')
    plt.legend()

    y = loadData(files_txt[15])
    plt.plot(x, y, color='orange', alpha=0.6, label='16')
    plt.legend()

    y = loadData(files_txt[16])
    plt.plot(x, y, color='gold', alpha=0.6, label='17')
    plt.legend()

    y = loadData(files_txt[17])
    plt.plot(x, y, color='yellow', alpha=0.6, label='18')
    plt.legend()

    y = loadData(files_txt[18])
    plt.plot(x, y, color='greenyellow', alpha=0.6, label='19')
    plt.legend()

    y = loadData(files_txt[19])
    plt.plot(x, y, color='coral', alpha=0.6, label='20')
    plt.legend()

    y = loadData(files_txt[20])
    plt.plot(x, y, color='palegreen', alpha=0.6, label='21')
    plt.legend()

    y = loadData(files_txt[21])
    plt.plot(x, y, color='seagreen', alpha=0.6, label='22')
    plt.legend()

    y = loadData(files_txt[22])
    plt.plot(x, y, color='lightseagreen', alpha=0.6, label='23')
    plt.legend()

    y = loadData(files_txt[23])
    plt.plot(x, y, color='teal', alpha=0.6, label='24')
    plt.legend()

    y = loadData(files_txt[24])
    plt.plot(x, y, color='lime', alpha=0.6, label='25')
    plt.legend()

    y = loadData(files_txt[25])
    plt.plot(x, y, color='cyan', alpha=0.6, label='26')
    plt.legend()

    y = loadData(files_txt[26])
    plt.plot(x, y, color='skyblue', alpha=0.6, label='27')
    plt.legend()

    y = loadData(files_txt[27])
    plt.plot(x, y, color='plum', alpha=0.6, label='28')
    plt.legend()

    y = loadData(files_txt[28])
    plt.plot(x, y, color='violet', alpha=0.6, label='29')
    plt.legend()

    y = loadData(files_txt[29])
    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,dpi=200, bbox_inches='tight')
    plt.show()
    
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值