【python】plt.plot() 函数绘制loss曲线
短信预约 -IT技能 免费直播动态提醒
目录
3. plt.plot(x, y, "格式控制字符串", 关键字=参数)
一. 数据形式 (输入数据)
训练过程中每个epoch都输出当前轮结果,输出数据保存在.txt文件,形式如下:
因为只是举个例子,只用30张图跑了5个epoch,不过数值不重要!过程先搞明白。
#每个epoch都输出当前轮结果print("epoch[%d/%d],train_loss,%.4f,train_acc,%.4f,train_miou,%.4f,eval_loss,%.4f,eval_acc,%.4f,eval_miou,%.4f,lr,%.6f,time,%ds" % (epoch + 1, EPOCHES, epoch_loss, epoch_acc, epoch_miou, val_loss, val_acc, val_miou, learning_rate, time.time() - st_epoch))#输出结果形式epoch[1/5],train_loss,3.0900,train_acc,0.0190,train_miou,0.0046,eval_loss,3.0398,eval_acc,0.0438,eval_miou,0.0075,lr,0.000005,time,23sepoch[2/5],train_loss,2.9437,train_acc,0.0667,train_miou,0.0090,eval_loss,2.9367,eval_acc,0.1133,eval_miou,0.0082,lr,0.000005,time,25sepoch[3/5],train_loss,2.8345,train_acc,0.1875,train_miou,0.0141,eval_loss,2.8379,eval_acc,0.2588,eval_miou,0.0087,lr,0.000005,time,25sepoch[4/5],train_loss,2.7256,train_acc,0.3285,train_miou,0.0163,eval_loss,2.7290,eval_acc,0.4454,eval_miou,0.0072,lr,0.000005,time,24sepoch[5/5],train_loss,2.6142,train_acc,0.5298,train_miou,0.0167,eval_loss,2.6255,eval_acc,0.5811,eval_miou,0.0032,lr,0.000005,time,27s
二. 绘图(完整代码)
- 逐行读取数据
- 用split()按所给符号分割
- 把所需列的数据按索引添加到对应的表里
- 用plt画图,需要什么形式的图,函数的参数可以参考第三部分函数介绍自行更改
#完整代码import matplotlib.pyplot as pltfile = open('log.txt') #打开文档lines = file.readlines() #读取文档数据#epoch = list(1, range(len(lines))+1) #epoch可以直接赋值,不放心的就用下面epoch的代码epoch = []train_loss = []val_loss = []for line in lines:# split用于将每一行数据用自定义的符号(我用的是逗号)分割成多个对象 # 取分割后的第0列,转换成float格式后添加到epoch列表中 epoch.append(str(line.split(',')[0])) # 取分割后的第2列,转换成float格式后添加到train_loss列表中 train_loss.append(float(line.split(',')[2])) #取分割后的第8列,转换成float格式后添加到val_loss列表中 val_loss.append(float(line.split(',')[8]))plt.figure() plt.title('loss during training') #标题plt.plot(epoch, train_loss, label="train_loss")plt.plot(epoch, val_loss, label="valid_loss")plt.legend()plt.grid()plt.show()
输出结果:
三. plt.plot() 函数 (调整图形)
matplotlib.pyplot模块下的一个函数, 用于画图。它可以绘制点和线, 并且对其样式进行控制。
函数定义为plt.plot(*args, **kwargs)
import matplotlib.pyplot as plthelp(plt.plot) # 查看英文函数定义
- *args, 可变位置参数, 以元组形式存放了很多无名参数.
- **kwargs, 可变关键字参数, 以字典形式存放了很多关键字及参数. 调用时可传入
- *args要放在**kwargs之前.
- 本函数的*args, 允许传入多对x和y和一个可选的"格式控制字符串".
- 本函数的**kwargs, 允许传入多个可选的关键字参数.
1. plt.plot(x, y)
- x为x轴数据, y为y轴数据。
- x, y可传入(元组), [列表], np.array, pd.Series。
- 也可同时传入多组。
import matplotlib.pyplot as pltimport numpy as npimport pandas as pd#示例一:x为x轴数据, y为y轴数据x=[3,4,5] # [列表]y=[2,3,2] # x,y元素个数N应相同plt.plot(x,y)plt.show()#示例二:x, y可传入(元组), [列表], np.array, pd.Seriesx=(3,4,5) # (元组)y1=np.array([3,4,3]) # np.arrayy2=pd.Series([4,5,4]) # pd.Seriesplt.plot(x,y1)plt.plot(y2) # x可省略,默认[0,1..,N-1]递增plt.show() # plt.show()前可加多个plt.plot(),画在同一张图上#示例三:可传入多组x, yx=(3,4,5)y1=np.array([3,4,3])y2=pd.Series([4,5,4])plt.plot(x,y1,x,y2) # 此时x不可省略plt.show()
结果示例:
2. plt.plot(x, y, "格式控制字符串")
点和线的格式可以用"格式控制字符串"设置,"最多可以包括三部分, "颜色", "点型", "线型"。
2.1 "颜色"与"线型"
如果只控制"颜色", 格式控制字符串还可以输入英文全称, 如"red", 甚至是十六进制RGB字符串, 如"#FF0000". python可用的"颜色"大全
============= =============================== character color ============= =============================== ``'b'`` blue 蓝 ``'g'`` green 绿 ``'r'`` red 红 ``'c'`` cyan 蓝绿 ``'m'`` magenta 洋红 ``'y'`` yellow 黄 ``'k'`` black 黑 ``'w'`` white 白 ============= ===============================
============= =============================== character description ============= =============================== ``'-'`` solid line style 实线 ``'--'`` dashed line style 虚线 ``'-.'`` dash-dot line style 点画线 ``':'`` dotted line style 点线 ============= ===============================
#示例import numpy as npimport pandas as pdimport matplotlib.pyplot as pltcolor=['b','g','r','c','m','y','k','w']linestyle=['-','--','-.',':']dic1=[[0,1,2],[3,4,5]]x=pd.DataFrame(dic1)dic2=[[2,3,2],[3,4,3],[4,5,4],[5,6,5]]y=pd.DataFrame(dic2)# 循环输出所有"颜色"与"线型"for i in range(2): for j in range(4): plt.plot(x.loc[i],y.loc[j],color[i*4+j]+linestyle[j]) plt.show()
输出结果:
2.2 "点型"
============= =============================== character description ============= =============================== ``'.'`` point marker ``','`` pixel marker ``'o'`` circle marker ``'v'`` triangle_down marker ``'^'`` triangle_up marker ``'<'`` triangle_left marker ``'>'`` triangle_right marker ``'1'`` tri_down marker ``'2'`` tri_up marker ``'3'`` tri_left marker ``'4'`` tri_right marker ``'s'`` square marker ``'p'`` pentagon marker ``'*'`` star marker ``'h'`` hexagon1 marker ``'H'`` hexagon2 marker ``'+'`` plus marker ``'x'`` x marker ``'D'`` diamond marker ``'d'`` thin_diamond marker ``'|'`` vline marker ``'_'`` hline marker ============= ===============================
#示例import numpy as npimport pandas as pdimport matplotlib.pyplot as pltmarker=['.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_','.',',']dic1=[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14],[15,16,17]]x=pd.DataFrame(dic1)dic2=[[2,3,2.5],[3,4,3.5],[4,5,4.5],[5,6,5.5]]y=pd.DataFrame(dic2)# 循环输出所有"点型"for i in range(6): for j in range(4): plt.plot(x.loc[i],y.loc[j],"b"+marker[i*4+j]+":") # "b"蓝色,":"点线plt.show()
3. plt.plot(x, y, "格式控制字符串", 关键字=参数)
本函数的**kwargs, 允许传入多个可选的关键字参数
#示例y=[2,3,2,4,5] # 青色,线宽10,星星,点尺寸50,点填充绿色,点边缘宽度6,点边缘青色plt.plot(y,color="c",linewidth=10,marker="*",markersize=20, markerfacecolor="g",markeredgewidth=3,markeredgecolor="c")plt.show()
函数用法是总结的这篇文章和matplotlib.pyplot.plot()参数详解两篇
来源地址:https://blog.csdn.net/m0_70813473/article/details/129838384
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341