How to slice a numpy array with inequalities?

I did my best to find a solution on my own, but I just do not raise anything important. I have several numpy arrays that are extracted from .tbl files (this is an astronomical table format that I extract using atpy). Each array has about 25,000 numbers, and I draw them as a scatter plot with matplotlib. The values ​​of the Y axis come directly from the array, the x axis is a simple subtraction of the values ​​in two separate arrays.

Which is all good, but I really need to do this, to extract values ​​that fall into a certain range (for example, I need values ​​for y that fall between 10 and 13, and x values ​​between 0 and 1). Of course, for the conspiracy to work, these values ​​must coincide with each other. Here is what I have:

import numpy as np from numpy import ndarray import matplotlib.pyplot as plt import matplotlib import atpy twomass = atpy.Table() twomass.read('/Raw_Data/IRSA_downloads/2MASS_GCbox1.tbl') hmag = list([twomass['h_m']]) jmag = list([twomass['j_m']]) colorjh = list([jh for j,h in zip(jmag, hmag)]) x = [] y = [] def list_maker(): for c,h in zip(colorjh, hmag): if np.all(c) < 1 == True and np.all(c) > 0 == True and np.all(h) < 13 == True and np.all(h) > 10 == True: x.append(c) y.append(h) list_maker() plt.scatter(x, y, c='g', s=1, alpha=0.05) plt.xlabel('Color(JH)', fontsize=15) #adjust axis labels here plt.ylabel('Magnitude (H)', fontsize=15) plt.gca().invert_yaxis() plt.legend(loc=2) plt.title('CMD for Galactic Center (2MASS)', fontsize=20) plt.grid(True) plt.show() 

I also tried this method for slicing data, but no luck:

 colorjh = colorjh[colorjh<1] colorjh = colorjh[colorjh>0] 

I tried many different things, including trying to turn these arrays into lists and many different things with inequality formatting. In this process, I may have walked away from the answer, but this code at least prints the entire spread (it cannot reduce the data as I want). Some other iterations printed blank stories anywhere near the range of numbers I'm looking for.

I am new to python and on this site, so please be as clear as possible with any tips, etc. If it's super-jargon, I may not be able to make good use of your offer. Thanks to all.

+4
source share
2 answers

Try the following: I think it does the same as your for and zip s, but should be much faster. If something doesn't make sense, ask in the comments and I will edit:

 hmag = np.array(hmag) jmag = np.array(jmah) colorjh = jmag - hmag idx_c = (colorjh > 0) & (colorjh < 1) # where condition on c is met idx_h = (hmag > 10) & (hmag < 13) # where condition on h is met idx = idx_c & idx_h # where both conditions are met plt.scatter(colorjh[idx], hmag[idx], c='g', s=1, alpha=0.05) 
+7
source

Both conditions can be met immediately:

 hmag = np.array(hmag) jmag = np.array(jmah) colorjh = jmag - hmag idx = ((colorjh > 0) & (colorjh < 1) & (hmag > 10) & (hmag < 13)).nonzero()[0] plt.scatter(colorjh[idx], hmag[idx], c='g', s=1, alpha=0.05) 

This .nonzero()[0] makes it a list of indices, not a β€œmask” with the values ​​True and False, this can be more efficient if we talk about very long lists.

+2
source

All Articles