2017-08-17 252 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