from astropy.io import fits
from scipy.ndimage import gaussian_filter
import numpy as np
import matplotlib.pyplot as plt
import pdb
# Create empty image
nx, ny = 512, 512
image = np.zeros((ny, nx))
# Set number of stars
n = 10000
# Generate random positions
r = np.random.random(n) * nx
theta = np.random.uniform(0., 2. * np.pi, n)
# Generate random fluxes
f = np.random.random(n) ** 2
# Compute position
x = nx / 2 + r * np.cos(theta)
y = ny / 2 + r * np.sin(theta)
# Add stars to image
# ==> First for loop and if statement <==
for i in range(n):
#pdb.set_trace()
if x[i] >= 0 and x[i] < nx and y[i] >= 0 and y[i] < ny:
#print(i)
#pdb.set_trace()
image[y[i], x[i]] += f[i]
#print(i)
# Convolve with a gaussian
image = gaussian_filter(image, 1)
# Add noise
image += np.random.normal(3., 0.01, image.shape)
# Write out to FITS image
fits.writeto('cluster.fits', image, clobber=True)
可能在i = 3和i = 4之间发生错误? 它出现了这样的消息
IndexError:只有整数,切片(
:
),省略号(...
),numpy.newaxis(None
)和整数或布尔数组才是有效索引
有没有人帮助我?
答案 0 :(得分:0)
您必须将y[i]
和x[i]
设为int:
image[int(y[i]),int(x[i])]
确定不是浮点数。