import matplotlib.pyplot as plt
import numpy as np
nmax=100
x = np.linspace(0,10,nmax)
y = 100*np.random.rand(nmax)
limiter = 4
plt.plot(x,y, 'k.-')
plt.axhline(70, c='m', ls='--')
plt.axhline(20, c='m', ls='--')
plt.grid()
#Perform linear interpolation
from scipy import interpolate
f = interpolate.interp1d(x, y)
new_nmax = 1000 #arbitrary value
xnew = np.linspace(0,10,nmax*new_nmax)
ynew = f(xnew)
#Failed attempt
mask=ynew>limiter
plt.plot(xnew[mask],ynew[mask], 'b-')
plt.plot(xnew[~mask],ynew[~mask], 'r-')
ynew
upper = 70
lower = 10
s = ynew.copy()
t = xnew.copy()
supper = np.ma.masked_where(s < upper, s)
slower = np.ma.masked_where(s > lower, s)
smiddle = np.ma.masked_where((s < lower) | (s > upper), s)
fig, ax = plt.subplots()
ax.plot(t, smiddle, t, slower, t, supper)
#plt.grid()
plt.show()