Oz Nahum wrote:
> Hi,
> I can't find a way to do a logarithmic regression in matplotlib,
> This can be done relatively easily in spread sheets like gnumeric and
> excel.
> Has anyone got a clue how to do it ?
> Thanks, Oz.
>
Matplotlib handles the graphics. For numeric regressions and fitting you
should use scipy, such as scipy's least square fit. I don't know if
scipy has a logarithmic regression predefined, but you should be able to
adapt the example below to your needs. This example shows how to fit a
gaussian to some noisy data.
import numpy as np
import numpy.random as random
from scipy.optimize.minpack import leastsq
import pylab as pl
x = np.arange(5.0,5.0,0.1)
y = 100.0*np.exp(x**2/25.0)+ 10.0*(random.random(len(x))0.5)
def resid(p,y,x):
A,sigma=p
return yA*np.exp((x/sigma)**2)
ls = leastsq(resid,[1.0,1.0],args=(y,x))
pl.plot(x,y,".",label="data")
y_fit = ls[0][0]*np.exp(x**2/ls[0][1]**2)
pl.plot(x,y_fit,"k",linewidth=1.5,label="fit")
pl.legend()
pl.show()
