Permalink
Browse files

Merge pull request #1010 from dmcdougall/offset_line

Port part of errorfill from Tony Yu's mpltools.
  • Loading branch information...
2 parents 3419eb8 + e8f18f9 commit 486a0cdb365a77dee469bd4f48daa58d6bc4dd6c @mdboom mdboom committed Aug 23, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 lib/matplotlib/mlab.py
View
30 lib/matplotlib/mlab.py
@@ -3194,3 +3194,33 @@ def quad2cubic(q0x, q0y, q1x, q1y, q2x, q2y):
c2x, c2y = c1x + 1./3. * (q2x - q0x), c1y + 1./3. * (q2y - q0y)
# c3x, c3y = q2x, q2y
return q0x, q0y, c1x, c1y, c2x, c2y, q2x, q2y
+
+def offset_line(y, yerr):
+ """
+ Offsets an array *y* by +/- an error and returns a tuple (y - err, y + err).
+
+ The error term can be:
+
+ o A scalar. In this case, the returned tuple is obvious.
+ o A vector of the same length as *y*. The quantities y +/- err are computed
+ component-wise.
+ o A tuple of length 2. In this case, yerr[0] is the error below *y* and
+ yerr[1] is error above *y*. For example::
+
+ from pylab import *
+ x = linspace(0, 2*pi, num=100, endpoint=True)
+ y = sin(x)
+ y_minus, y_plus = mlab.offset_line(y, 0.1)
+ plot(x, y)
+ fill_between(x, ym, y2=yp)
+ show()
+
+ """
+ if cbook.is_numlike(yerr) or (cbook.iterable(yerr) and len(yerr) == len(y)):
+ ymin = y - yerr
+ ymax = y + yerr
+ elif len(yerr) == 2:
+ ymin, ymax = y - yerr[0], y + yerr[1]
+ else:
+ raise ValueError("yerr must be scalar, 1xN or 2xN")
+ return ymin, ymax

0 comments on commit 486a0cd

Please sign in to comment.