[SciPy-dev] gplt suggestion: initcmd
Alan G Isaac
aisaac at american.edu
Fri Jul 15 20:27:49 EDT 2005
On Fri, 15 Jul 2005, Fernando Perez apparently wrote:
> 3. Change the scipy and plotting tutorials to use matplotlib.
Here's the figure 1 tutorial.
(Looks nice!)
Cheers,
Alan Isaac
x = arange(0,6e-2,6e-2/30)
A,k,theta = 10, 1.0/3e-2, pi/6
y_true = A*sin(2*pi*k*x+theta)
y_meas = y_true + 2*randn(len(x))
def residuals(p, y, x):
A,k,theta = p
err = y-A*sin(2*pi*k*x+theta)
return err
def peval(x, p):
return p[0]*sin(2*pi*p[1]*x+p[2])
p0 = [8, 1/2.3e-2, pi/3]
print array(p0)
#[ 8. 43.4783 1.0472]
from scipy.optimize import leastsq
plsq = leastsq(residuals, p0, args=(y_meas, x))
print plsq[0]
# random outcome
print array([A, k, theta])
#[ 10. 33.3333 0.5236]
from pylab import * # Requires Matplotlib
plot(x,peval(x,plsq[0]),x,y_meas,'o',x,y_true)
title('Least-squares fit to noisy data')
legend(['Fit', 'Noisy', 'True'])
savefig(r'c:\leastsqfit.eps') # Make eps file.
More information about the SciPy-Dev
mailing list