目录
一. 数据形式 (输入数据)
二. 绘图(完整代码)
三. plt.plot() 函数 (调整图形)
1. plt.plot(x, y)
2. plt.plot(x, y, "格式控制字符串")
2.1 "颜色"与"线型"
2.2 "点型"
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,23s
epoch[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,25s
epoch[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,25s
epoch[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,24s
epoch[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 plt
file = 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 plt
help(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 plt
import numpy as np
import 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.Series
x=(3,4,5) # (元组)
y1=np.array([3,4,3]) # np.array
y2=pd.Series([4,5,4]) # pd.Series
plt.plot(x,y1)
plt.plot(y2) # x可省略,默认[0,1..,N-1]递增
plt.show() # plt.show()前可加多个plt.plot(),画在同一张图上
#示例三:可传入多组x, y
x=(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 np
import pandas as pd
import matplotlib.pyplot as plt
color=['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 np
import pandas as pd
import matplotlib.pyplot as plt
marker=['.',',','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://www.toymoban.com/news/detail-463921.html
python中的颜色表文章来源地址https://www.toymoban.com/news/detail-463921.html
到了这里,关于【python】plt.plot() 函数绘制loss曲线的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!