可视化: Python—MatPlotLib—折线图带子图

文章目录

图示

在这里插入图片描述

代码

import  matplotlib.pyplot as plt   
import numpy as np
def plot_epoch_for_performance_and_loss(model_name, res_dict):                                        
    """Function: 评价指标以及训练集loss和epoch的关系曲线                                              
    - param:                                                                                          
        model_name: (str) 模型的名称                                                                  
        res_dict: (dict) 包含loss, 表现随epoch变化的list                                              
    """                                                                                               
    color = ['r', 'g', 'b', 'y']                                                                 
    shape = ['o', 'v', '^']                                                                           
    loss = res_dict['epoch_loss']                                                                     
    fig = plt.figure(figsize=(15,6)) # figsize指定给个图大小(两个数字分别表示横轴纵轴)                 
    ax1 = fig.add_subplot(1, 2, 1) # 1行2列的图,相当于四个图,1是第一个                              
    ax2 = fig.add_subplot(1, 2, 2) # 1行2列的图,相当于四个图,3是第三个                              
    ax1.plot(np.arange(len(loss)), np.array(loss))                                                   
    ax1.set_xlabel("Epoch")                                                                               
    ax1.set_ylabel("Loss")                                                                                
                                                                                                      
    legend = []                                                                                       
    for idx, key in enumerate(list(res_dict.keys())):                                                  
        if 'loss' in key:                                                                             
            continue                                                                                  
        c = color[idx%len(color)]                                                                     
        s = shape[idx%len(shape)]                                                                     
        ax2.plot(np.arange(len(res_dict[key])), np.array(res_dict[key]), color=c, marker=s)            
        legend.append(key)                                                                            
    ax2.legend(legend)                                                                                
    ax2.set_xlabel("Epoch")                                                                               
    ax2.set_ylabel("Perfermance")                                                                         
                                                                                                      
    plt.show()                                                                                        
    plt.savefig(model_name + '.png')    

model_name = 'BertCrf'
res_dict = {
    'epoch_loss':[10,6,5,5,3,2,1],
    'train_f1':[1,2,3,4,5,6,7],
    'dev_f1':[1,3,5,4,5,1,2,3]
}     

plot_epoch_for_performance_and_loss(model_name, res_dict)                                                             
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值