How to calculate the number of scatterplot data points in a particular 'region' of the graph

Viewed 20

As my questions says I'm trying to find a way to calculate the number of scatterplot data points (pink dots) in a particular 'region' of the graph or either side of the black lines/boundaries. Open to any ideas as I don't even know where to start. Thank you!!

The code:


################################
############ GES  ##############     
################################
p = fits.open('GES_DR17.fits')      
       
pfeh = p[1].data['Fe_H']      
pmgfe = p[1].data['Mg_Fe']              
pmnfe = p[1].data['Mn_Fe']        
palfe = p[1].data['Al_Fe']  
    
#Calculate [(MgMn]
pmgmn = pmgfe - pmnfe        
 
ax1a.scatter(palfe, pmgmn, c='thistle', marker='.',alpha=0.8,s=500,edgecolors='black',lw=0.3, vmin=-2.5, vmax=0.65) 


ax1a.plot([-1,-0.07],[0.25,0.25], c='black')  
ax1a.plot([-0.07,1.0],[0.25,0.25], '--', c='black')   
   
x = np.arange(-0.15,0.4,0.01)  
ax1a.plot(x,4.25*x+0.8875, 'k', c='black') 


enter image description here

1 Answers

Let's call the two axes x and y. Any line in this plot can be written as

a*x + b*y + c = 0

for some value of a,b,c. But if we plug in a points with coordinates (x,y) in to the left hand side of the equation above we get positive value for all points of the one side of the line, and a negative value for the points on the other side of the line. So If you have multiple regions delimited by lines you can just check the signs. With this you can create a boolean mask for each region, and just count the number of Trues by using np.sum.

# assign the coordinates to the variables x and y as numpy arrays
x = ...
y = ...
line1 = a1*x + b1*y + c1
line2 = a2*x + b2*y + c2
mask = (line1 > 0) & (line2 < 0)  # just an example, signs might vary
count = np.sum(mask)
Related