python读取txt文件数字内容并绘svg曲线图,新罗马字体

这段代码读取'epoch_loss.txt'和'epoch_val_loss.txt'文件中的损失数据,将训练损失和验证损失绘制成曲线图。使用matplotlib库,设置字体为'Times New Roman',并保存图像为'epoch_loss.png'和'epoch_loss.svg',确保矢量图不失真。
摘要由CSDN通过智能技术生成
from matplotlib import pyplot as plt

plt.rcParams.update({
    'font.family': 'sans-serif',
    'font.sans-serif': ['Times New Roman'],
    })
losses= []
val_loss = []


with open('epoch_loss.txt', encoding='utf-8') as f:
    for line in f:
        line = line.strip('\n')
        print(line)
        losses.append(float(line))
        num=len(losses)
        iters = range(num)
with open('epoch_val_loss.txt', encoding='utf-8') as f:
    for line in f:
        line = line.strip('\n')
        val_loss.append(float(line))


plt.figure()
plt.plot(iters, losses, 'red', linewidth=2, label='train loss')
plt.plot(iters, val_loss, 'blue', linewidth=2, label='val loss')
print(iters)
print(losses)
plt.grid(True)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('A Loss Curve')
plt.legend(loc="upper right")

plt.savefig("epoch_loss.png")
plt.savefig( "epoch_loss.svg")  #矢量图不失真

plt.cla()
plt.close("all")

效果图如下

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值