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:

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?
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 = '--')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With