Josh Marsh
Josh Marsh

Reputation: 21

Hiding all points above a plotted line in a scatter plot

Plotting 2 lists of galaxy equivalent width ratios, Im plotting them against each other then I want to eliminate all points above the line y=(0.61)/(x-0.05)+1.3 so that only points below remain in the plot. Here is what I have so far and I've looked on stack overflow but am lost on how to make this happen. Note: I only want the points above y not y2 to be eliminated, thanks!

plt.scatter(NT,OT,s=5, c='r', marker='o')
x = np.linspace(-5,5,100)
y=(0.61)/(x-0.05)+1.3
y2 =((0.61)/(x-0.47)) + 1.19#
plt.plot(x, y, '-k')
plt.plot(x, y2, '--k')
plt.xlabel('log(NII/HA)', color='#1C2833')
plt.ylabel('log(OIII/HB)', color='#1C2833')
plt.ylim((-1,1.5))   
plt.xlim((-2,0.5))  

thanks in advance any advice would be appreciated!

Upvotes: 2

Views: 868

Answers (1)

William Miller
William Miller

Reputation: 10328

I believe the easiest way to do this will be to simply filter the desired points out before plotting, you can do so with

filteredx = list()
filteredy = list()
for ii in range(len(NT)):
    if OT[ii] < (0.61) / (NT[ii] - 0.05) + 1.3:    #Adjust equation as needed
        filteredx.append(NT[ii])
        filteredy.append(OT[ii])
plt.scatter(filteredx, filteredy)

For a complete example:

import numpy as np
import matplotlib.pyplot as plt

# Create random data for demonstration
np.random.seed(1)
n = 200
datay = np.random.randn(n)*50
datax = np.random.randn(n)*1.5
x = np.linspace(-5, 5, 1000)
y = (0.61) / (x - 0.05) + 1.3

filteredx = list()
filteredy = list()
plt.scatter(datax, datay, marker='o', color='g', label="All Points")
for ii in range(n):
    if datay[ii] < (0.61) / (datax[ii] - 0.05) + 1.3:
        filteredx.append(datax[ii])
        filteredy.append(datay[ii])
plt.scatter(filteredx, filteredy, marker='.', color='r', label="After Filtering")
plt.plot(x,y, linewidth=1, linestyle='-')
plt.legend()
plt.show()

Filtering demonstration

To remove the asymptote you can filter for values larger than and less than x = 0.05 and plot the two groups separately, i.e.

plt.plot(x[x < 0.05], y[x < 0.05], linewidth=1, linestyle='-', color='b')
plt.plot(x[x > 0.05], y[x > 0.05], linewidth=1, linestyle='-', color='b')

Filtering below line

You do in this case need to specify the color, otherwise the two regions will have different colors. Additionally, if you need to label the line for the legend, simply give one of the two a label - if you give one to each region you will end up getting two legend entries.

Upvotes: 1

Related Questions