0

I'm following an example in the photutils documentation to detect sources in an image:

from astropy.stats import sigma_clipped_stats
from photutils.datasets import load_star_image
import numpy as np
import matplotlib.pyplot as plt
from astropy.visualization import SqrtStretch
from astropy.visualization.mpl_normalize import ImageNormalize
from photutils.detection import DAOStarFinder
from photutils.aperture import CircularAperture
    
# Load image
hdu = load_star_image() # load a star image from the dataset
data = hdu.data[0:101, 0:101]

mean, median, std = sigma_clipped_stats(data, sigma = 3.0) # estimate noise

# Find stars in the image that have FWHMs of 3 pixels and peaks ~ 5 sigma > bg
daofind = DAOStarFinder(fwhm = 3.0, threshold = 5.*std)
sources = daofind(data - median)

# Print position and photometric data for each star in the image
for col in sources.colnames:
    sources[col].info.format = '%.8g' # for consistent table output

positions = np.transpose((sources['xcentroid'], sources['ycentroid']))
apertures = CircularAperture(positions, r = 4.)

norm = ImageNormalize(stretch = SqrtStretch())
plt.imshow(data, cmap = 'Greys', origin = 'lower', norm = norm,
           interpolation = 'nearest')

for i in range(len(sources)):
    if sources[i][-1] < -2:
        print(sources[i][-1])
        apertures.plot(color = 'r', lw = 1.5, alpha = 0.5

Which produces

enter image description here

I've added the last four lines, with the intention to plot apertures around only the brightest stars. However, the for loop doesn't change the image. I understand why (it's plotting all apertures multiple times, once for each of the 4 stars with mag < -2), but how do I change it to plot them for only those stars?

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Jim421616
  • 1,434
  • 3
  • 22
  • 47
  • 1
    This looks like a typo: you're iterating over sources in the for loop, but you're using all apertures in each iteration; you could solve this by plotting only `apertures[i]` instead of `apertures` – keflavich Nov 07 '21 at 01:36
  • Thanks, that's it. – Jim421616 Nov 07 '21 at 19:44

0 Answers0