Skip to content

Commit 486a0cd

Browse files
committed
Merge pull request #1010 from dmcdougall/offset_line
Port part of errorfill from Tony Yu's mpltools.
2 parents 3419eb8 + e8f18f9 commit 486a0cd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/matplotlib/mlab.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,3 +3194,33 @@ def quad2cubic(q0x, q0y, q1x, q1y, q2x, q2y):
31943194
c2x, c2y = c1x + 1./3. * (q2x - q0x), c1y + 1./3. * (q2y - q0y)
31953195
# c3x, c3y = q2x, q2y
31963196
return q0x, q0y, c1x, c1y, c2x, c2y, q2x, q2y
3197+
3198+
def offset_line(y, yerr):
3199+
"""
3200+
Offsets an array *y* by +/- an error and returns a tuple (y - err, y + err).
3201+
3202+
The error term can be:
3203+
3204+
o A scalar. In this case, the returned tuple is obvious.
3205+
o A vector of the same length as *y*. The quantities y +/- err are computed
3206+
component-wise.
3207+
o A tuple of length 2. In this case, yerr[0] is the error below *y* and
3208+
yerr[1] is error above *y*. For example::
3209+
3210+
from pylab import *
3211+
x = linspace(0, 2*pi, num=100, endpoint=True)
3212+
y = sin(x)
3213+
y_minus, y_plus = mlab.offset_line(y, 0.1)
3214+
plot(x, y)
3215+
fill_between(x, ym, y2=yp)
3216+
show()
3217+
3218+
"""
3219+
if cbook.is_numlike(yerr) or (cbook.iterable(yerr) and len(yerr) == len(y)):
3220+
ymin = y - yerr
3221+
ymax = y + yerr
3222+
elif len(yerr) == 2:
3223+
ymin, ymax = y - yerr[0], y + yerr[1]
3224+
else:
3225+
raise ValueError("yerr must be scalar, 1xN or 2xN")
3226+
return ymin, ymax

0 commit comments

Comments
 (0)