Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6651210

Browse files
committed
fix unit changes for errorbar_limits code
svn path=/trunk/matplotlib/; revision=4097
1 parent 3c83fac commit 6651210

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

lib/matplotlib/axes.py

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3505,7 +3505,7 @@ def pie(self, x, explode=None, labels=None,
35053505
texts = []
35063506
slices = []
35073507
autotexts = []
3508-
for frac, label, expl in zip(x,labels, explode):
3508+
for frac, label, expl in cbook.safezip(x,labels, explode):
35093509
x, y = center
35103510
theta2 = theta1 + frac
35113511
thetam = 2*math.pi*0.5*(theta1+theta2)
@@ -3645,11 +3645,11 @@ def errorbar(self, x, y, yerr=None, xerr=None,
36453645

36463646
if xerr is not None:
36473647
if not iterable(xerr):
3648-
xerr = [xerr]
3648+
xerr = [xerr]*len(x)
36493649

36503650
if yerr is not None:
36513651
if not iterable(yerr):
3652-
yerr = [yerr]
3652+
yerr = [yerr]*len(y)
36533653

36543654
l0 = None
36553655

@@ -3679,6 +3679,18 @@ def errorbar(self, x, y, yerr=None, xerr=None,
36793679
if not iterable(xuplims): xuplims = npy.array([xuplims]*len(x), bool)
36803680
else: xuplims = npy.asarray(xuplims, bool)
36813681

3682+
def xywhere(xs, ys, mask):
3683+
"""
3684+
return xs[mask], ys[mask] where mask is True but xs and
3685+
ys are not arrays
3686+
"""
3687+
assert len(xs)==len(ys)
3688+
assert len(xs)==len(mask)
3689+
xs = [thisx for thisx, b in zip(xs, mask) if b]
3690+
ys = [thisy for thisy, b in zip(ys, mask) if b]
3691+
return xs, ys
3692+
3693+
36823694
if capsize > 0:
36833695
plot_kw = {
36843696
'ms':2*capsize,
@@ -3691,53 +3703,66 @@ def errorbar(self, x, y, yerr=None, xerr=None,
36913703
if xerr is not None:
36923704
if iterable(xerr) and len(xerr)==2:
36933705
# using list comps rather than arrays to preserve units
3694-
left = [thisx-thiserr for (thisx, thiserr) in zip(x,xerr[0])]
3695-
right = [thisx+thiserr for (thisx, thiserr) in zip(x,xerr[1])]
3706+
left = [thisx-thiserr for (thisx, thiserr) in cbook.safezip(x,xerr[0])]
3707+
right = [thisx+thiserr for (thisx, thiserr) in cbook.safezip(x,xerr[1])]
36963708
else:
36973709
# using list comps rather than arrays to preserve units
3698-
left = [thisx-thiserr for (thisx, thiserr) in zip(x,xerr)]
3699-
right = [thisx+thiserr for (thisx, thiserr) in zip(x,xerr)]
3710+
left = [thisx-thiserr for (thisx, thiserr) in cbook.safezip(x,xerr)]
3711+
right = [thisx+thiserr for (thisx, thiserr) in cbook.safezip(x,xerr)]
37003712

37013713
barcols.append( self.hlines(y, left, right, **lines_kw ) )
37023714
if capsize > 0:
37033715
if xlolims.any():
3704-
caplines.extend( self.plot(left[xlolims], y[xlolims], ls='None', marker=mlines.CARETLEFT, **plot_kw) )
3716+
# can't use numpy logical indexing since left and
3717+
# y are lists
3718+
leftlo, ylo = xywhere(left, y, xlolims)
3719+
3720+
caplines.extend( self.plot(leftlo, ylo, ls='None', marker=mlines.CARETLEFT, **plot_kw) )
37053721
xlolims = ~xlolims
3706-
caplines.extend( self.plot(left[xlolims], y[xlolims], 'k|', **plot_kw) )
3722+
leftlo, ylo = xywhere(left, y, xlolims)
3723+
caplines.extend( self.plot(leftlo, ylo, 'k|', **plot_kw) )
37073724
else:
37083725
caplines.extend( self.plot(left, y, 'k|', **plot_kw) )
37093726

37103727
if xuplims.any():
3711-
caplines.extend( self.plot(right[xuplims], y[xuplims], ls='None', marker=mlines.CARETRIGHT, **plot_kw) )
3728+
3729+
rightup, yup = xywhere(right, y, xuplims)
3730+
caplines.extend( self.plot(rightup, yup, ls='None', marker=mlines.CARETRIGHT, **plot_kw) )
37123731
xuplims = ~xuplims
3713-
caplines.extend( self.plot(right[xuplims], y[xuplims], 'k|', **plot_kw) )
3732+
rightup, yup = xywhere(right, y, xuplims)
3733+
caplines.extend( self.plot(rightup, yup, 'k|', **plot_kw) )
37143734
else:
37153735
caplines.extend( self.plot(right, y, 'k|', **plot_kw) )
37163736

37173737
if yerr is not None:
37183738
if iterable(yerr) and len(yerr)==2:
37193739
# using list comps rather than arrays to preserve units
3720-
lower = [thisy-thiserr for (thisy, thiserr) in zip(y,yerr[0])]
3721-
upper = [thisy+thiserr for (thisy, thiserr) in zip(y,yerr[1])]
3740+
lower = [thisy-thiserr for (thisy, thiserr) in cbook.safezip(y,yerr[0])]
3741+
upper = [thisy+thiserr for (thisy, thiserr) in cbook.safezip(y,yerr[1])]
37223742
else:
37233743
# using list comps rather than arrays to preserve units
3724-
lower = [thisy-thiserr for (thisy, thiserr) in zip(y,yerr)]
3725-
upper = [thisy+thiserr for (thisy, thiserr) in zip(y,yerr)]
3744+
lower = [thisy-thiserr for (thisy, thiserr) in cbook.safezip(y,yerr)]
3745+
upper = [thisy+thiserr for (thisy, thiserr) in cbook.safezip(y,yerr)]
37263746

37273747
barcols.append( self.vlines(x, lower, upper, **lines_kw) )
37283748
if capsize > 0:
37293749

37303750
if lolims.any():
3731-
caplines.extend( self.plot(x[lolims], lower[lolims], ls='None', marker=mlines.CARETDOWN, **plot_kw) )
3751+
xlo, lowerlo = xywhere(x, lower, lolims)
3752+
caplines.extend( self.plot(xlo, lowerlo, ls='None', marker=mlines.CARETDOWN, **plot_kw) )
37323753
lolims = ~lolims
3733-
caplines.extend( self.plot(x[lolims], lower[lolims], 'k_', **plot_kw) )
3754+
xlo, lowerlo = xywhere(x, lower, lolims)
3755+
caplines.extend( self.plot(xlo, lowerlo, 'k_', **plot_kw) )
37343756
else:
37353757
caplines.extend( self.plot(x, lower, 'k_', **plot_kw) )
37363758

37373759
if uplims.any():
3738-
caplines.extend( self.plot(x[uplims], upper[uplims], ls='None', marker=mlines.CARETUP, **plot_kw) )
3760+
xup, upperup = xywhere(x, upper, uplims)
3761+
3762+
caplines.extend( self.plot(xup, upperup, ls='None', marker=mlines.CARETUP, **plot_kw) )
37393763
uplims = ~uplims
3740-
caplines.extend( self.plot(x[uplims], upper[uplims], 'k_', **plot_kw) )
3764+
xup, upperup = xywhere(x, upper, uplims)
3765+
caplines.extend( self.plot(xup, upperup, 'k_', **plot_kw) )
37413766
else:
37423767
caplines.extend( self.plot(x, upper, 'k_', **plot_kw) )
37433768

lib/matplotlib/cbook.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,15 @@ def report_memory(i=0): # argument may go away
844844

845845
return mem
846846

847+
848+
def safezip(x, y):
849+
'make sure x and y are equal len before zipping'
850+
Nx = len(x)
851+
Ny = len(y)
852+
if Nx!=Ny:
853+
raise RuntimeError('x and y must be equal length; found len(x)=%d and len(y)=%d'%
854+
(Nx, Ny))
855+
return zip(x, y)
847856
class MemoryMonitor:
848857
def __init__(self, nmax=20000):
849858
self._nmax = nmax

0 commit comments

Comments
 (0)