Q:

Locating region between a confidence interval in data using Python

I have some data that looks like this:

The code to generate this plot:
CI=4.5
data=pandas.DataFrame([3,5,1,2,3,4,5,6])
plt.figure()
plt.plot(data)
plt.plot([CI]*len(data),'--')
plt.ylabel('y data',fontsize=15)
plt.xlabel('x data',fontsize=15)
plt.title('example data',fontsize=15)

I can also interpolate the data using scipy:
from scipy.interpolate import interp1d
f=interp1d(x_data,y_data,kind='linear')
x_interp=numpy.linspace(min(x_data),max(x_data), num=100*len(x_data), endpoint=True)
y_interp=f(x_interp)        
plt.figure()
plt.plot(x_interp,y_interp)

The green line represents a confidence interval. I need to programmatically locate the x values where the y values cross this confidence internal. But, the complication is that since this is a confidence interval I need to get the values that cross the green line in both directions: 

i.e. I need the x values where the y data crosses the red arrows whilst excluding those at the black arrow. I've tried many variations of subtracting the confidence interval from the interpolated data as well as taking the absolute values but I still cannot isolate the confidence interval.

A:

Based on

I want to x values for which the y values lie within a specific range

it seems you're simply looking for a mask. If you give numpy a relational statement, e.g. my_array > x, it will return a boolean array with True for any indices where this relation is satisfied. And if you pass such a mask to an array for indexing, it'll return the values where this mask is True. For example,
In [2]: a = np.array([1, 3, 2, 5, 2, 9, 4])

In [3]: a > 2
Out[3]: array([False,  True, False,  True, False,  True,  True], dtype=bool)

In [4]: a[a > 2]
Out[4]: array([3, 5, 9, 4])

So, to find the x values where f(x) lies within a specific range, find the indices where f(x) is in the desired range and filter your x based on that mask.
# multiplication between 1s and 0s acts like logical AND
mask = (y_interp >= lower_bound) * (y_interp <= upper_bound)
accepted_parameters = x_interp[mask]

