import numpy as np from astropy.modeling import models, fitting # Generate fake data np.random.seed(42) g1 = models.Gaussian1D(1, 0, 0.2) g2 = models.Gaussian1D(2.5, 0.5, 0.1) x = np.linspace(-1, 1, 200) y = g1(x) + g2(x) + np.random.normal(0., 0.2, x.shape) # Now to fit the data create a new superposition with initial # guesses for the parameters: gg_init = models.Gaussian1D(1, 0, 0.1) + models.Gaussian1D(2, 0.5, 0.1) fitter = fitting.SLSQPLSQFitter() gg_fit = fitter(gg_init, x, y) # Plot the data with the best-fit model plt.figure(figsize=(8,5)) plt.plot(x, y, 'ko') plt.plot(x, gg_fit(x), 'r-', lw=2) plt.xlabel('Position') plt.ylabel('Flux')