python - 在我实际点击之前调用“Button_Press_Event”坐标
问题描述
我正在绘制一片星空的拟合图像。然后,我在足够亮的恒星上显示图像上的圆形光圈。接下来,我试图点击我感兴趣的星星,并从最近的圆圈中获取亮度测量值。最后,我想将该亮度测量用于其他计算。
我的问题是,我声明用于存储单击的 x、y 坐标的变量(“坐标”)似乎在我实际单击之前被调用,从而导致空数组和错误。
几个月前,一位同事将它寄给我,曾几何时它完美无瑕。但它似乎停止了工作。这可能是更新某些库/模块的结果,但我不能确定。
我尝试了各种组合,包括在其他功能、其他位置等声明“坐标”。我尝试更改某些行的顺序,以便稍后调用“坐标”。我也尝试从头开始编写代码,并且遇到了许多相同的错误。因为这个特定的代码曾经有效,所以我附上了它而不是我的尝试。
老实说,我没有完全理解代码,因为我没有编写它。结果,我不明白什么时候被调用,或者我所做的任何更改实际上做了什么。
def plot_image(file, vmin=5, vmax=99, threshold=5, radius=25):
hdu=fits.open(file)
image = hdu[0].data
exptime = hdu[0].header['EXPTIME']
band = hdu[0].header['FILTERS']
airmass = hdu[0].header['AIRMASS']
readnoise = hdu[0].header['RN_01']
gain = hdu[0].header['GAIN_01']
obj = hdu[0].header['OBJECT']
sub = image[1500:2000,1500:2000]
bkg_sigma = mad_std(sub)
mean, median, std = sigma_clipped_stats(sub, sigma=3.0, maxiters=5)
daofind = photutils.DAOStarFinder(fwhm=2., threshold=threshold*bkg_sigma)
sources = daofind(sub - median)
positions = (sources['xcentroid'], sources['ycentroid'])
apertures = photutils.CircularAperture(positions, r=radius)
phot_table = photutils.aperture_photometry(sub - median, apertures)
pix = select(image, apertures)
print(pix)
if len(pix) == 2 or len(coords) == 2:
distance = np.sqrt((np.array(phot_table['xcenter'])-pix[0])**2 + (np.array(phot_table['ycenter'])-pix[1])**2)
star = np.argmin(dist)
counts = phot_table[star]['aperture_sum']
fluxfile = open('testfile.txt')
signal = (counts * gain) / exptime
err = np.sqrt(counts*gain + (readnoise**2*np.pi*radius**2))
else:
print('Pix length = 0')
def select(image, apertures, vmin = 5, vmax = 99):
global coords
coords = []
fig = plt.figure(figsize = (9,9))
ax = fig.add_subplot(111)
ax.imshow(image, cmap = 'gist_gray_r', origin='lower', vmin = np.percentile(image, vmin), vmax = np.percentile(image, vmax), interpolation='none')
apertures.plot(color='blue', lw=1.5, alpha=0.5, ax = ax)
ax.set_title('Hello')#label='Object: '+obj+'\nFilter: '+band)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
fig.canvas.mpl_disconnect(cid)
if None in coords:
return [np.nan,np.nan]
else:
return np.round(coords)
def onclick(event):
x = event.xdata
y = event.ydata
global coords
coords = [x, y]
plt.close()
return
def closeonclick(event):
print('Close On Click')
plt.close()
return
预期结果:显示的图像覆盖有蓝色光圈。然后,我单击所需的星,我单击的坐标将存储到“坐标”并打印到控制台。显示图像的窗口也与上一步一起关闭。最后,使用这些坐标,它会找到最近的光圈,并对产生的亮度进行一些科学处理。
实际结果:立即打印“坐标”(一个空列表)。紧接着,图像显示出来。点击它什么都不做。它不会更改“坐标”的值,不会打印任何其他内容,也不会关闭窗口。
解决方案
如果它不正确,我会回来删除它(如果我有声誉,我会发表评论),但看起来你必须先在任何函数之外定义一个全局变量,然后在变量名之前使用关键字在函数内部更改其范围。尝试将“coords = []”移到您的函数之外(第一次调用“onclick”后列表将不再为空,但每次新的点击都应该替换坐标,所以这不应该成为问题)。
参考:https ://www.programiz.com/python-programming/global-keyword
推荐阅读
- azure - 如何捕获 azure 的限制异常?
- excel - 图表对象的粘贴方法在 Excel 2016 中无法按预期工作
- c# - 如何从arduino检索值到asp.net
- optimization - Dart 2 中的最佳渲染循环是什么?
- postgresql - 使用 SQLX 转到可选字段
- apache-spark - Spark Kafka 在 spark 2.3.0 中使用 python 流式传输
- java - 无法使用 Selenium WebDriver 上传许可证文件
- ms-access - Microsoft Access:查询以查找数据差异
- entity-framework - EF Core - 阅读器关闭时调用 CheckDataIsReady 的尝试无效(OutOfMemory)
- python-3.x - Python 3 - Base64 编码