import matplotlib.pyplot as plt
import numpy as np
from astropy.modeling.models import Gaussian2D
from photutils.centroids import centroid_2dg
from photutils.datasets import make_noise_image
from photutils.profiles import CurveOfGrowth

# Create an artificial single source
gmodel = Gaussian2D(42.1, 47.8, 52.4, 4.7, 4.7, 0)
yy, xx = np.mgrid[0:100, 0:100]
data = gmodel(xx, yy)
bkg_sig = 2.1
noise = make_noise_image(data.shape, mean=0., stddev=bkg_sig, seed=0)
data += noise
error = np.zeros_like(data) + bkg_sig

# Find the source centroid
xycen = centroid_2dg(data)

# Create the curve of growth
radii = np.arange(1, 26)
cog = CurveOfGrowth(data, xycen, radii, error=error)

# Plot the curve of growth
fig, ax = plt.subplots(figsize=(8, 6))
cog.plot(ax=ax, label='Curve of Growth')
cog.plot_error(ax=ax)
ax.legend()