mpl.mlab.cross_from_below()

mpl.mlab.cross_from_below(x, threshold)[source]

return the indices into x where x crosses some threshold from below, e.g., the i’s where:

x[i-1]<threshold and x[i]>=threshold

Example code:

import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.1)
s = np.sin(2*np.pi*t)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t, s, '-o')
ax.axhline(0.5)
ax.axhline(-0.5)

ind = cross_from_below(s, 0.5)
ax.vlines(t[ind], -1, 1)

ind = cross_from_above(s, -0.5)
ax.vlines(t[ind], -1, 1)

plt.show()