前言:为了避免每次绘图时都要反复输入linewidth=2,marksize=4这类参数导致的代码很长的问题,采用了一种更为美观,便于统一理解的方法。
实现原理与方法
首先我们来看一下ply.plot的使用说明:
输入
help(ply.plot)
重点看 call signatures:
plot(*args, scalex=True, scaley=True, data=None, **kwargs)
Plot y versus x as lines and/or markers.
Call signatures::
plot([x], y, [fmt], *, data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
参数的传入方式 *args和**kwargs两种。
关于python函数中 * 和**的理解可以参考Python函数中*和**的内涵究竟是什么呢?
plt.figure(num = 'Train', figsize =(16,12), edgecolor = 'k', frameon = True)
plot_list = {'markersize': 8, 'linewidth':2} # 统一设置标记点大小和线宽
linestyle_list = ['r-+', 'g-o', 'b-*','y-^', 'c-v', 'm-x'] # 线条颜色及样式列表
plt.subplot(211)
plt.plot(np.arange(n_samples), y_train,color = 'k', label='True y',**plot_list) # **plot_list实现对plot_list的解包
for i, pre_y in enumerate(y_train_pre):
plt.plot(np.arange(n_samples), pre_y,
linestyle_list[i], label = model_names[i],**plot_list)
font1 = {'family':'Time New Roman','weight':'normal', 'size':24}
font2 = {'family':'Time New Roman','weight':'normal', 'size':12}
plt.title('Regression result comparsion',font1)
plt.legend(loc = 'upper right', prop =font2)
plt.xlabel('Number', font1)
plt.ylabel('Real and prediction values(μm)', font1)
#设置坐标刻度值的大小
plt.tick_params(labelsize=23)