Not expected result received with scipy's curve_fit

I was trying to do multivariate logarithmic regression on my data with scipy curve_fit and as a result expect to get a line, but get a curve. Here is the code I used :

Quercetin=[23,195,6,262,272,158,79,65,136,198]
Naringenin=[11,4,8,6,6,7,6,9,7,9]
Rutin=[178,165,93,239,202,3325,4427,7607,3499,1762]
TEAC=[23,189,37,265,290,267,362,388,364,321]

import matplotlib.pyplot as plt
import scipy
from scipy.optimize import curve_fit
import numpy as np
def func(x, a, b, c, d,e):
    m=np.log(a*x[0]+b*x[1]+c*x[2])
    return d*(m)+e
x=scipy.array([Quercetin, Naringenin,Rutin])
y=scipy.array(TEAC)
popt, pcov = curve_fit(func, x ,y)
print (popt)

plt.plot(func(x,*popt),y,'ro-')
plt.show()

And I get this result: enter image description here

while I want to get something like this :

enter image description here

Could anyone please give me a hint on what I am doing wrong? If this matters I use Python 3.5 from Anaconda on Windows 10.

Upvotes: 0

Views: 53

Answers (1)

ev-br
ev-br

Reputation: 26030

(I'm assuming that the question is really about plotting). You are asking matplotlib to plot red dots ('ro') connected by straight lines (-). Matplotlib obliges and connects them in whatever order they are given.

If you want to plot a line, just plot it separately:

In [58]: yres = func(x, *popt)

In [59] plt.plot(yres, y, 'ro')
Out[59]: [<matplotlib.lines.Line2D at 0x7f5c0796b828>]

In [60]: plt.plot([0, 400], [0, 400], '-')
Out[60]: [<matplotlib.lines.Line2D at 0x7f5c07a9c860>]

Upvotes: 2

Related Questions