errorbar ( xdata, ydata, yerr = yerr, fmt = 'k.' ) # Data plt. plot ( xdata, powerlaw ( xdata, amp, index )) # Fit plt. sqrt ( covar ) * amp # Plotting data # plt. leastsq ( errfunc, pinit, args = ( logx, logy, logyerr ), full_output = 1 ) pfinal = out covar = out print pfinal print covar index = pfinal amp = 10.0 ** pfinal inde圎rr = np. log10 ( ydata ) logyerr = yerr / ydata # define our (line) fitting function fitfunc = lambda p, x : p + p * x errfunc = lambda p, x, y, err : ( y - fitfunc ( p, x )) / err pinit = out = optimize. # y = a * x^b # log(y) = log(a) + b*log(x) # logx = np. # Note that the `logyerr` term here is ignoring a constant prefactor. # Fitting the data - Least Squares Method # Power-law fitting is best done by first converting # to a linear equation and then fitting to a straight line. leastsq ( errorfunction, params ) return p shape )) - data ) p, success = optimize. max () return height, x, y, width_x, width_y def fitgaussian ( data ): """Returns (height, x, y, width_x, width_y) the gaussian parameters of a 2D distribution found by a fit""" params = moments ( data ) errorfunction = lambda p : np. exp ( - ((( center_x - x ) / width_x ) ** 2 + (( center_y - y ) / width_y ) ** 2 ) / 2 ) def moments ( data ): """Returns (height, x, y, width_x, width_y) the gaussian parameters of a 2D distribution by calculating its moments """ total = data. transAxes )ĭef gaussian ( height, center_x, center_y, width_x, width_y ): """Returns a gaussian function with the given parameters""" width_x = float ( width_x ) width_y = float ( width_y ) return lambda x, y : height * np. text ( 0.8, 0.07, 'x freq : %.3f kHz' % ( 1 / p ), fontsize = 16, horizontalalignment = 'center', verticalalignment = 'center', transform = ax. legend (( 'x position', 'x fit', 'y position', 'y fit' )) ax = plt. title ( "Oscillations in the compressed trap" ) plt. plot ( Ty, tY, "b^", time, fitfunc ( p, p, time ), "b-" ) # Legend the plot plt. plot ( Tx, tX, "ro", time, fitfunc ( p, p, time ), "r-" ) # Plot of the second data and the fit time = np. max (), 100 ) # Plot of the first data and the fit plt. leastsq ( errfunc, p, args = ( Tx, tX, Ty, tY )) time = np. errfunc = lambda p, x1, y1, x2, y2 : r_, p, x1 ) - y1, fitfunc ( p, p, x2 ) - y2 ] # This time we need to pass the two sets of data, there are thus four "args". pi / T * x + p ) + p * x # Initial guess for the first set's parameters p1 = r_ # Initial guess for the second set's parameters p2 = r_ # Initial guess for the common period T = 0.8 # Vector of the parameters to fit, it contains all the parameters of the problem, and the period of the oscillation is not there twice ! p = r_ # Cost function of the fit, compare it to the previous example. # Target function fitfunc = lambda T, p, x : p * np. text ( 0.8, 0.07, 'x freq : %.3f kHz \n y freq : %.3f kHz' % ( 1 / p1, 1 / p2 ), fontsize = 16, horizontalalignment = 'center', verticalalignment = 'center', transform = ax. plot ( Ty, tY, "b^", time, fitfunc ( p2, time ), "b-" ) # Legend the plot plt. leastsq ( errfunc, p0, args = ( Ty, tY )) time = np. plot ( Tx, tX, "ro", time, fitfunc ( p1, time ), "r-" ) # Plot of the data and the fit # Fit the second set p0 = p2, success = optimize. leastsq ( errfunc, p0, args = ( Tx, tX )) time = np. pi / p * x + p ) + p * x # Target function errfunc = lambda p, x, y : fitfunc ( p, x ) - y # Distance to the target function p0 = # Initial guess for the parameters p1, success = optimize. # Fit the first set fitfunc = lambda p, x : p * np.