用matplotlib库绘制光滑的曲线图
注意点:坐标要是用range()表示的话用这个方法拟合平滑曲线会报错,查了别人说的把range()转化成list(range())感觉并没有用,所以还是用了穷举法表示的x坐标。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline, interp1d
from tensorflow.python.keras.metrics import Reduce
#plot double lines
def plot_double_lines(n, x, y1, y2, pic_name):
# initialize plot parameters
print('picture name: %s, len of data: %d' % (pic_name, n))
plt.rcParams['figure.figsize'] = (6 * 8 / 5, 6)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
plt.xticks(np.arange(0,10,2),size=15)
plt.yticks(np.arange(0,1.1,0.1),size=15)
plt.xlabel("epoch", fontsize=15)
# plt.ylabel("acc", fontsize=15)
# plot curve 1
plt.plot(x, y1, label='Score')
# plot curve 2
plt.plot(x, y2, label='Similarity')
# show the legend
plt.legend()
# show the picture
plt.show()
if __name__ == '__main__':
xs = np.array([0,1,2,3,4,5])
y1s = np.array([0.79, 0.82, 1, 1,1,1])
y2s = np.array([0.9, 0.8, 0.75, 0.41, 0.03,1])
xnew = np.linspace(xs.min(), xs.max(), 300)
func = interp1d(xs, y1s, kind='cubic')
func2 = interp1d(xs, y2s, kind='cubic')
ynew = func(xnew)
ynew2 = func2(xnew)
plot_double_lines(51, xnew, ynew, ynew2, 'Two Curves')