常见的绘图模板,各种字体的调整都在代码中。
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
import matplotlib.pyplot as plt
# from matplotlib import rcParams
result1 = []
result2 = []
result3 = []
result4 = []
result5 = []
result6 = []
result7 = []
result8 = []
with open('0.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result1.append(float(value_list[0]))
except:
pass
with open('1.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result2.append(float(value_list[0]))
except:
pass
with open('2.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result3.append(float(value_list[0]))
except:
pass
with open('3.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result4.append(float(value_list[0]))
except:
pass
with open('4.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result5.append(float(value_list[0]))
except:
pass
with open('5.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result6.append(float(value_list[0]))
except:
pass
with open('6.txt', 'r') as f:#1
lines = f.readlines()#2
for line in lines:#3
line = line.split(':')[1]
try:
if line:
value_list = line.split(',')
# print(value_list)
# X.append(int(value[0]))
result7.append(float(value_list[0]))
except:
pass
fig, ax = plt.subplots()
x = []
for i in range(300):
x.append(i+1)
# 标签参数设置
# params = {
# 'font.serif':'Times New Roman',
# # 'figure.dpi': 300,
# # 'savefig.dpi': 300,
# 'font.size': 240,
# }
# plt.rcParams.update(params)
# 曲线参数设置
line_width = 5 # 线宽
plt.plot(x[:300], result1[:300], '#1f77b4', label='0', linewidth=line_width)
plt.plot(x[:300], result2[:300], '#ff7f0e', label='1', linewidth=line_width)
plt.plot(x[:300], result3[:300], '#2ca02c', label='2', linewidth=line_width)
plt.plot(x[:300], result4[:300], '#9467bd', label='3', linewidth=line_width)
plt.plot(x[:300], result5[:300], '#8c564b', label='4', linewidth=line_width)
plt.plot(x[:300], result6[:300], '#bcbd22', label='5', linewidth=line_width)
plt.plot(x[:300], result7[:300], '#d62728', label='6', linewidth=line_width)
# plt.legend(loc='best')
plt.legend(prop={'family': 'Times New Roman', 'size': 24})
# plt.title(u"loss", family='Times New Roman', size=22)
plt.xlabel(u'训练次数', family='sans-serif', size=18)
plt.ylabel(u'损失值', family='sans-serif', fontsize=18)
# plt.xlabel(u'Epochs', family='Times New Roman', size=20)
# plt.ylabel(u'Loss', family='Times New Roman', fontsize=20)
plt.yticks(fontproperties='Times New Roman', size=16)
plt.xticks(fontproperties='Times New Roman', size=16)
plt.xlim(0, 300)
plt.ylim(0, 20)
plt.show()
fig.savefig('./scatter.jpg', dpi=600, format='jpg')