def test_guess_from_peak2d(): """Regression test for guess_from_peak2d function (see GH #627).""" x = np.linspace(-5, 5) y = np.linspace(-5, 5) amplitude = 0.8 centerx = 1.7 sigmax = 0.3 centery = 1.3 sigmay = 0.2 z = lineshapes.gaussian2d(x, y, amplitude=amplitude, centerx=centerx, sigmax=sigmax, centery=centery, sigmay=sigmay) model = models.Gaussian2dModel() guess_increasing_x = model.guess(z, x=x, y=y) guess_decreasing_x = model.guess(z[::-1], x=x[::-1], y=y[::-1]) assert guess_increasing_x == guess_decreasing_x for param, value in zip(['centerx', 'centery'], [centerx, centery]): assert np.abs((guess_increasing_x[param].value - value) / value) < 0.5
from lmfit.lineshapes import gaussian2d, lorentzian ############################################################################### # Two-dimensional Gaussian # ------------------------ # We start by considering a simple two-dimensional gaussian function, which # depends on coordinates `(x, y)`. The most general case of experimental # data will be irregularly sampled and noisy. Let's simulate some: npoints = 10000 np.random.seed(2021) x = np.random.rand(npoints) * 10 - 4 y = np.random.rand(npoints) * 5 - 3 z = gaussian2d(x, y, amplitude=30, centerx=2, centery=-.5, sigmax=.6, sigmay=.8) z += 2 * (np.random.rand(*z.shape) - .5) error = np.sqrt(z + 1) ############################################################################### # To plot this, we can interpolate the data onto a grid. X, Y = np.meshgrid(np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)) Z = griddata((x, y), z, (X, Y), method='linear', fill_value=0) fig, ax = plt.subplots() art = ax.pcolor(X, Y, Z, shading='auto') plt.colorbar(art, ax=ax, label='z') ax.set_xlabel('x')