import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.font_manager import FontProperties
import csv
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import make_interp_spline
# def smooth_xy(lx, ly):# """数据平滑处理# :param lx: x轴数据,数组# :param ly: y轴数据,数组# :return: 平滑后的x、y轴数据,数组 x_smooth, y_smooth# """# x = np.array(lx)# y = np.array(ly)# x_smooth = np.linspace(x.min(), x.max(), 100)# y_smooth = make_interp_spline(x, y)(x_smooth)# return x_smooth, y_smooth'''平滑'''defsmooth_xy(scalars, weight):# Weight between 0 and 1
last = scalars[0]# First value in the plot (first timestep)
smoothed =list()for point in scalars:
smoothed_val = last * weight +(1- weight)* point # Calculate smoothed value
smoothed.append(smoothed_val)# Save it
last = smoothed_val # Anchor the last smoothed valuereturn smoothed
'''读取csv文件'''defreadcsv(files):
csvfile =open(files,'r')
plots = csv.reader(csvfile, delimiter=',')
x =[]
y =[]#读取csv文件中2,3列的数据,且转化为float类型for row in plots:
y.append(float(row[2]))
x.append(float(row[1]))return x ,y
mpl.rcParams['font.family']='sans-serif'
mpl.rcParams['font.sans-serif']='NSimSun,Times New Roman'
plt.figure()#读取文件
x2,y2=readcsv("xxx.csv")
y2_=smooth_xy(y2,0.6)
plt.plot(x2, y2, color='gray', label='10%')
plt.plot(x2, y2_, color='red', label='10%')
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)#ylim和xlim的最大最小值根据csv文件的极大极小值决定
plt.ylim(0,1.5)
plt.xlim(9,50)
plt.xlabel('epoch',fontsize=20)
plt.ylabel('weight',fontsize=20)
plt.legend(fontsize=16)
plt.show()