In [24]:
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import gaussian_filter

#  numpy function
random=np.random
pi=np.pi
cos=np.cos
sin=np.sin
zeros=np.zeros
# Create empty image
nx, ny = 512, 512
image = zeros((ny, nx))

# Set number of stars
n = 10000

# Generate random positions
r = random.random(n) * nx
theta = random.uniform(0., 2. * pi, n)

# Generate random fluxes
f = random.random(n) ** 2

# Compute position
x = nx / 2 + r * cos(theta)
y = ny / 2 + r * sin(theta)

# Add stars to image
# ==> First for loop and if statement <==
for i in range(n):
    if x[i] >= 0 and x[i] < nx and y[i] >= 0 and y[i] < ny:
        image[y[i],x[i]]+= f[i]

# Convolve with a gaussian
image = gaussian_filter(image, 1)

# Add noise
image += random.normal(3., 0.01, image.shape)

# Write out to FITS image
pits.writeto('cluster.fits', image, clobber=True)


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-24-085de95efc1b> in <module>()
     32 for i in range(n):
     33     if x[i] >= 0 and x[i] < nx and y[i] >= 0 and y[i] < ny:
---> 34         image[y[i],x[i]]+= f[i]
     35 
     36 # Convolve with a gaussian

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

I can not solve the problem that the IndexError in the line 34..


In [ ]: