0

I'm trying to draw a curve for regression fitting. The curve is for a higher degree polynomial ( 6 and above ).

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

I want it to be a smooth curve.

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

2
  • Were did best_coef come from?? Did you write this? Commented Aug 17, 2017 at 17:57
  • i've written a polyfit function which returns the coefficient of best degree polynomial that fits the data. Commented Aug 18, 2017 at 7:16

1 Answer 1

1

I think you just need to sort x_train before plotting the fit results:

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

The plot you included looks like the x_train values (and therefore also the fitted values) are in random order, but the plot routine does not connect the nearest points, but consecutive points in the arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.