import numpy as np from astropy.modeling import models, fitting # Generate fake data np.random.seed(0) x = np.linspace(-5., 5., 200) y = 3 * np.exp(-0.5 * (x - 1.3)**2 / 0.8**2) y += np.random.normal(0., 0.2, x.shape) # Fit the data using a box model t_init = models.Trapezoid1D(amplitude=1., x_0=0., width=1., slope=0.5) fit_t = fitting.LevMarLSQFitter() t = fit_t(t_init, x, y) # Fit the data using a Gaussian g_init = models.Gaussian1D(amplitude=1., mean=0, stddev=1.) fit_g = fitting.LevMarLSQFitter() g = fit_g(g_init, x, y) # Plot the data with the best-fit model plt.figure(figsize=(8,5)) plt.plot(x, y, 'ko') plt.plot(x, t(x), 'b-', lw=2, label='Trapezoid') plt.plot(x, g(x), 'r-', lw=2, label='Gaussian') plt.xlabel('Position') plt.ylabel('Flux') plt.legend(loc=2)