From: <jd...@us...> - 2008-12-17 14:57:32
|
Revision: 6648 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6648&view=rev Author: jdh2358 Date: 2008-12-17 14:57:28 +0000 (Wed, 17 Dec 2008) Log Message: ----------- added some threshold crossing helper funcs to mlab Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/mlab.py Modified: trunk/matplotlib/lib/matplotlib/mlab.py =================================================================== --- trunk/matplotlib/lib/matplotlib/mlab.py 2008-12-17 14:55:42 UTC (rev 6647) +++ trunk/matplotlib/lib/matplotlib/mlab.py 2008-12-17 14:57:28 UTC (rev 6648) @@ -54,6 +54,16 @@ yourself stranded without scipy (and the far superior scipy.integrate tools) +:meth:`contiguous_regions` + return the indices of the regions spanned by some logical mask + +:meth:`cross_from_below` + return the indices where a 1D array crosses a threshold from below + +:meth:`cross_from_above` + return the indices where a 1D array crosses a threshold from above + + record array helper functions ------------------------------- @@ -3236,6 +3246,63 @@ boundaries.append((in_region, i+1)) return boundaries + +def cross_from_below(x, threshold): + """ + return the indices into *x* where *x* crosses some threshold from + below, eg 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() + + .. seealso:: + + :func:`cross_from_above` and :func:`contiguous_regions` + + """ + x = np.asarray(x) + threshold = threshold + ind = np.nonzero( (x[:-1]<threshold) & (x[1:]>=threshold))[0] + if len(ind): return ind+1 + else: return ind + +def cross_from_above(x, threshold): + """ + return the indices into *x* where *x* crosses some threshold from + below, eg the i's where:: + + x[i-1]>threshold and x[i]<=threshold + + .. seealso:: + + :func:`cross_from_below` and :func:`contiguous_regions` + + """ + x = np.asarray(x) + ind = np.nonzero( (x[:-1]>=threshold) & (x[1:]<threshold))[0] + if len(ind): return ind+1 + else: return ind + ################################################## # Vector and path length geometry calculations ################################################## This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |