1.绘图常用颜色
代码值 | 具体颜色 |
---|---|
‘b’ | 蓝色 |
‘g’ | 绿色 |
‘r’ | 红色 |
‘y’ | 黄色 |
‘k’ | 黑色 |
‘w’ | 白色 |
‘c’ | 青色 |
‘m’ | 品红色 |
2.绘图常用线性形状
代码值 | 具体形状 |
---|---|
’o-‘ | 实心圆 |
‘*-’ | 星号 |
‘.-’ | 圆点形 |
‘:’ | 细虚线 |
‘+’ | 加号 |
‘x’ | 叉号 |
‘-’ | 实线 |
‘–’ | 粗虚线 |
‘s’ | 方形 |
‘1’ | 下三角 |
‘2’ | 上三角 |
‘3’ | 左三角 |
‘4’ | 油三角 |
‘D’ | 钻石 |
‘d’ | 细钻石 |
‘|’ | 短竖线 |
‘_’ | 短横线 |
3.绘制多条折线图
3.1具体代码
import matplotlib.pyplot as plt
import numpy as np
epoch= np.arange(0,70,10)
acces = [0.65,0.78,0.79,0.85,0.90,0.95,0.98]
cls_loss = [0.06,0.07,0.075,0.085,0.09,0.095,0.099]
bbox_loss = [0.2,0.3,0.4,0.22,0.5,0.4,0.23]
landmark_loss = [0.9,0.8,0.6,0.5,0.4,0.3,0.2]
L2_loss = [0.1,0.2,0.3,0.35,0.25,0.3,0.1]
# 如果要显示中文或者负号就加入这两行
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 输出保存指定尺寸的图
plt.figure(figsize=(10, 5))
plt.plot(epoch, acces, 'o-', color='r', label='acc')
plt.plot(epoch, cls_loss, '1-', color='m', label='cls_loss')
plt.plot(epoch, bbox_loss, ':', color='b', label='bbox_loss')
plt.plot(epoch, landmark_loss, color='y', label='landmark_loss')
plt.plot(epoch, L2_loss, 'D-', color='g', label='L2_loss')
# 给折线图数据点加数值
for a, b in zip(epoch, acces):
plt.text(a, b, '%.1f'%b, ha='center', va= 'bottom', fontsize=10)
# 使用自定义的样式文件
plt.style.use(('dark_background'))
plt.xlabel('epoches',fontsize=10) # x轴表示
plt.ylabel('loss/acc',fontsize=10) # y轴表示
plt.title("折线图",fontsize=10) # 图标标题表示
plt.grid() # 显示网格
plt.legend(loc=1)
"""
每条折线的label显示
0:'best'
1: 'upper right'
2: 'upper left'
3:'lower left'
4: 'lower right'
5: 'right'
6: 'center left'
7: 'center right'
8: 'lower center'
9: 'upper center'
10: 'center'
"""
# 直接设置y轴上的每一个刻度值
# plt.yticks([0, 0.4, 0.8, 1.2, 1.4, 1.6, 1.8, 2.0])
# 设置横纵坐标的刻度范围,强行将x轴和y轴设置为下面的范围 合理分布每段距离
plt.xlim((0, 70))
plt.ylim((0, 1))
#######################
plt.tight_layout()
plt.savefig('testP.jpg',dpi=500,bbox_inches = 'tight') # 保存图片,路径名为test.jpg
plt.show() # 显示图片
plt.close()
AI 代码解读
3.2运行结果
4.设置双y轴和一个x轴的折线图
4.1具体代码
def plot_loss_and_lr(train_loss, learning_rate):
try:
x = list(range(len(train_loss)))
fig, ax1 = plt.subplots(1, 1)
ax1.plot(x, train_loss, 'r', label='loss')
ax1.set_xlabel("step")
ax1.set_ylabel("loss")
ax1.set_title("Train Loss and lr")
plt.legend(loc='best')
ax2 = ax1.twinx()
ax2.plot(x, learning_rate, label='lr')
ax2.set_ylabel("learning rate")
ax2.set_xlim(0, len(train_loss)) # 设置横坐标整数间隔
plt.legend(loc='best')
handles1, labels1 = ax1.get_legend_handles_labels()
handles2, labels2 = ax2.get_legend_handles_labels()
plt.legend(handles1 + handles2, labels1 + labels2, loc='upper right')
fig.subplots_adjust(right=0.8) # 防止出现保存图片显示不全的情况
fig.savefig('./loss_and_lr{}.png'.format(datetime.datetime.now().strftime("%Y%m%d-%H%M%S")))
plt.close()
print("successful save loss curve! ")
except Exception as e:
print(e)
AI 代码解读
# 传入两个列表 append添加
plot_loss_and_lr(train_loss, learning_rate)
AI 代码解读
4.2运行结果
中文显示
plt.rcParams[‘font.sans-serif’]=[‘FangSong’]
负号显示
plt.rcParams[‘axes.unicode_minus’]=False
觉得有用点赞支持一下❤