2017-08-17 128 views
0

我想绘制回归拟合曲线。该曲线适用于更高阶多项式(6以上)。如何在matplotlib python中绘制多项式曲线?

fig=figure() 
ax1=fig.add_axes((0.1,0.2,0.8,0.7)) 
ax1.set_title("Training data(blue) and fitting curve(red)") 
ax1.set_xlabel('X-axis') 
ax1.set_ylabel('Y-axis') 

ax1.plot(x_train,y_train,'.',x_train,np.polyval(best_coef,x_train),'-r') 
show() 

This is the output of the given code

我想这是一条平滑的曲线。

something like this , with a continues red line , instead of discreet point of red

+0

是否有'best_coef'来自?你写了这个吗? – wwii

+0

我写了一个polyfit函数,它返回适合数据的最佳度数多项式的系数。 – ajithalbus

回答

1

我想你只需要绘制的拟合结果前进行排序x_train

ax1.plot(x_train,y_train,'.', np.sort(x_train),np.polyval(best_coef,np.sort(x_train)),'-r') 

包括你看起来像x_train值(因此也拟合值),该地块是随机的顺序,但绘图程序不会连接最近的点,而是连接数组中的点。

+0

谢谢。它现在有效;) – ajithalbus