“ Button_Press_Event”在我实际单击之前被调用的坐标

问题描述 投票:0回答:1

我正在绘制星场的拟合图像。然后,我在足够明亮的恒星上显示图像上的圆形光圈。接下来,我尝试单击我感兴趣的星星,并从最近的圆圈中获取亮度测量值。最后,我想将该亮度测量值用于其他计算。

我的问题是,我声明要存储单击的x,y坐标(“坐标”)的变量似乎在我实际单击之前被调用,从而导致一个空数组和错误。

一位同事几个月前将其发送给我,从前,它工作得很完美。但似乎已停止工作。这可能是更新某些库/模块的结果,但是我不确定。

我尝试了各种组合,包括在其他函数中,在其他位置声明“ coords”。我尝试更改某些行的顺序以使“ coords”稍后被调用。我也尝试过从头开始编写代码,并且遇到了许多相同的错误。因为此特定代码曾经有效,所以我附上了它,而不是我的尝试。

老实说,我没有完全理解代码,因为我没有编写代码。结果,我不明白什么时候被调用,或者我实际上所做的任何更改。

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”的值,不会打印其他任何内容,也不会关闭窗口。

我正在绘制星场的拟合图像。然后,我在足够明亮的恒星上显示图像上的圆形光圈。接下来,我尝试单击我感兴趣的星星,...

python numpy matplotlib click
1个回答
0
投票

如果不正确,我会回来并删除它(如果我有声誉,我会发表评论),但是看起来您必须首先在任何函数之外定义一个全局变量,然后在函数内部的变量名以更改其范围。尝试将“ coords = []”移到函数外部(在第一次调用“ onclick”之后,列表将不再为空,但是每次单击均应替换坐标,因此不会出现问题)。

© www.soinside.com 2019 - 2024. All rights reserved.