 # ploting a logarithmic regression to scattered data ?

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.

···

``````        Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people

Living life in peace
``````

when one person suffers from a delusion it is called insanity. When many people suffer from a delusion it is called religion."
Robert Pirsig, Zen and the Art of Motorcycle Maintenance

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 y-A*np.exp(-(x/sigma)**2)

ls = leastsq(resid,[1.0,1.0],args=(y,x))

pl.plot(x,y,".",label="data")
y_fit = ls*np.exp(-x**2/ls**2)
pl.plot(x,y_fit,"k-",linewidth=1.5,label="fit")
pl.legend()
pl.show()