Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate plot values above and below a value

I'm graphing several points and I want to eliminate all values above and below a given range.

So i've plotted all my points as

import numpy as np
import matplotlib.pylab as plt

I have two arrays: "magnitude" and "color", and equations of best fit so im plotting as:

plt.scatter(magnitude,color)
plt.plot(x,equation)
plt.plot(x,equation +0.5, linestyle =  '--')
plt.plot(x,equation-0.5, linestyle = '--')

where x is just some linspace. It gives me a plot that looks like this: enter image description here

which is exactly what I want, but now I want to remove all of the points above and below the red dotted lines (which are equation +0.5, and equation -0.5) but I really have no clue how to do this. Tips?

like image 436
AWiltzer Avatar asked Mar 18 '26 12:03

AWiltzer


1 Answers

You can make use of indexing and create a mask defining your boundary conditions since your equation seems to be a NumPy array as you are performing arithmetic operation (+0.5) on it as

y1 = equation - 0.5
y2 = equation + 0.5

mask = (color>y1) & (color<y2)

plt.scatter(magnitude[mask], color[mask])
plt.plot(x, equation)
plt.plot(x, y1, linestyle =  '--')
plt.plot(x, y2, linestyle = '--')
like image 93
Sheldore Avatar answered Mar 21 '26 02:03

Sheldore