目录
一. 数据形式 (输入数据)
二. 绘图(完整代码)
三. 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
#完整代码
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()
输出结果:
matplotlib.pyplot模块下的一个函数, 用于画图。它可以绘制点和线, 并且对其样式进行控制。
函数定义为plt.plot(*args, **kwargs)
import matplotlib.pyplot as plt
help(plt.plot) # 查看英文函数定义
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()
结果示例:
点和线的格式可以用"格式控制字符串"设置,"最多可以包括三部分, "颜色", "点型", "线型"。
如果只控制"颜色", 格式控制字符串还可以输入英文全称, 如"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()
输出结果:
============= ===============================
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()
本函数的**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()参数详解两篇
python中的颜色表