Numpy 的条件

Conditions with Numpy

我有一个小问题。我找不到得到我想要的东西的方法。 我有一个数组。我创建变量 "Tri_1" 以便仅获取符合我条件的行。

但我还想从另一列中获取考虑先前条件的行,以便在 x 中绘制图形 (g0-r0) 并在 y 中绘制 g0。因此,只需获取 'g0' 列中的行,其中 Tri_1 有效。

Tri_1 = tbdata[((tbdata['g0-r0']) < 0.8) & (-0.4 < (tbdata['g0-r0']))]  # Ne garder que les -0.4 < (g-r)0 < 0.8

print tbdata[(tbdata['g0'], Tri_1)] 

我不知道我该怎么做..

我的输入文件是这样的:

ID          RA        DEC     NDET DEPTHFLAG SEPINDX [65] SEPFINDX [65]   U   UERR    G       GERR      R       RERR      I       IERR       Z       ZERR      CHI      SHARP   FLAG PROB    EBV   g0   r0   i0   z0   X   Y   g0-r0
------------- ---------- ---------- ---- --------- ------------ ------------- ----- ---- ------- --------- ------- --------- ------- ---------- ------- ---------- -------- --------- ---- ---- --------- ------- ------- ------- ------- ------- ------- -------- --------
Field169.1255 184.766833 -30.472462   17         3   1254 .. -1    1254 .. -1 99.99 9.99 21.4875 0.0100426 20.0497 0.0070361 18.9002 0.00376412 18.4352 0.00425852 0.911594 -0.212171   -1 0.87 0.0709362 21.257831573486328, 19.895366668701172, 18.787057876586914, 18.348880767822266, -0.23369831258206375, 0.98211750000000109, 1.3624649047851562

原因是我想在 (g0-r0) 的函数中绘制 g0。但是,如果我做:

fig2 = plt.figure(2)
plt.plot(Tri_1, tbdata['g0'], '.')
plt.title('Diagramme Couleur-Magnitude étoiles bleues')
plt.xlabel('(g0-r0)')
plt.ylabel('g0')
plt.xlim(-1.5,2.5)
plt.ylim(14,28)
plt.gca().invert_yaxis()
plt.show()

它不起作用,因为 x 和 y 的维度不同。所以我只想'g0'对应'g0-r0'

谢谢!

更新:

这是我到目前为止的发现:

#!/usr/bin/python
# coding: utf-8

from astropy.io import fits
from astropy.table import Table
from astropy.table import Column
import numpy as np
import matplotlib.pyplot as plt


    ###################################
    # Importation du fichier de champ #
    ###################################

filename = '/home/valentin/Desktop/Field169_combined_final_roughcal.fits_traite_traiteXY_traiteXY_final'

print 'Fichier en cours de traitement' + str(filename) + '\n'

# Ouverture du fichier à l'aide d'astropy
field = fits.open(filename)        

# Lecture des données fits 
tbdata = field[1].data            


    #######################################
    # Paramètres pour la carte de densité #
    #######################################

# Boite des étoiles bleues :
Blue_stars_X = tbdata[np.bitwise_and(tbdata['g0-r0'] > -0.5, tbdata['g0-r0'] < 0.8 )] # Ne garder que les -0.4 < (g-r)0 < 0.8
Blue_stars_Y = Blue_stars = Blue_stars_X[Blue_stars_X['g0'] < 23.5]

RA_Blue_stars = ?? # get values RA from previous conditions
DEC_Blue_stars = ?? # get values DEC from previous conditions

# Boite des étoiles très bleues :
Very_Blue_stars_X = tbdata[np.bitwise_and(tbdata['g0-r0'] > -0.5, tbdata['g0-r0'] < 0.2 )]
Very_Blue_stars_Y = Very_Blue_stars = Very_Blue_stars_X[Very_Blue_stars_X['g0'] < 23.5]



    #####################################
    # Traçage des différents graphiques #
    #####################################


fig1 = plt.figure(1)
plt.plot(tbdata['g0-r0'], tbdata['g0'], 'r.', label=u'Etoiles du champ')
plt.plot(Blue_stars['g0-r0'], Blue_stars['g0'], 'b.', label =u'Etoiles bleues')
plt.plot(Very_Blue_stars['g0-r0'], Very_Blue_stars['g0'], 'k.', label =u'Etoiles très bleues')
plt.title('Diagramme Couleur-Magnitude')
plt.xlabel('(g0-r0)')
plt.ylabel('g0')
plt.xlim(-1.5,2.5)
plt.ylim(14,28)
plt.legend(loc='upper left')
plt.gca().invert_yaxis()

plt.show()
#fig.savefig('CMD.png')

print "Création du Diagramme"

如果您只想包含满足条件的元素,假设:

condition = (tbdata['g0-r0']) < 0.8) & (-0.4 < (tbdata['g0-r0'])

那么您需要在两个轴上都使用条件,以便您只组合同一条目的元素。

x_data = tbdata[condition]['g0-r0']
y_data = tbdata[condition]['g0']
plt.plot(x_data, y_data, '.')

或者根据您的情况,您只需更改 plot 行:

plt.plot(Tri_1['g0-r0'], Tri_1['g0'], '.')

针对您的情况:

# Setup the condition:
condition1 = np.bitwise_and(tbdata['g0-r0'] > -0.5, tbdata['g0-r0'] < 0.8 )
# Combine it with the g0 condition
condition_final = np.bitwise_and(Blue_stars_X['g0'] < 23.5, condition1)
# Get all rows where the condition is true:
Blue_stars = tbdata[condition_final]

RA_Blue_stars = Blue_stars['RA']
DEC_Blue_stars = Blue_stars['DEC']

为了保持干净,我现在不关心忧郁,只需使用与上面类似的东西。

然后绘制它:

fig1 = plt.figure(1)
plt.plot(tbdata['g0-r0'], tbdata['g0'], 'r.', label=u'Etoiles du champ')
plt.plot(Blue_stars['g0-r0'], Blue_stars['g0'], 'b.', label =u'Etoiles bleues')
plt.title('Diagramme Couleur-Magnitude')
plt.xlabel('(g0-r0)')
plt.ylabel('g0')
plt.xlim(-1.5,2.5)
plt.ylim(14,28)
plt.legend(loc='upper left')
plt.gca().invert_yaxis()

plt.show()
#fig.savefig('CMD.png')