Skip to content

Commit 99eb8d2

Browse files
committed
FIX: do not assume we can [] error inputs
In errorbar do not assume that [0] or [1] will work correctly (due to issues with pandas). Closes pandas-dev/pandas#11858
1 parent 172706f commit 99eb8d2

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

lib/matplotlib/axes/_axes.py

+39-41
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
import matplotlib.cbook as cbook
1717
from matplotlib.cbook import (mplDeprecation, STEP_LOOKUP_MAP,
18-
iterable, is_string_like)
18+
iterable, is_string_like,
19+
safe_first_element)
1920
import matplotlib.collections as mcoll
2021
import matplotlib.colors as mcolors
2122
import matplotlib.contour as mcontour
@@ -2890,29 +2891,44 @@ def xywhere(xs, ys, mask):
28902891
if key in kwargs:
28912892
plot_kw[key] = kwargs[key]
28922893

2893-
if xerr is not None:
2894-
if (iterable(xerr) and len(xerr) == 2 and
2895-
iterable(xerr[0]) and iterable(xerr[1])):
2896-
# using list comps rather than arrays to preserve units
2897-
left = [thisx - thiserr for (thisx, thiserr)
2898-
in cbook.safezip(x, xerr[0])]
2899-
right = [thisx + thiserr for (thisx, thiserr)
2900-
in cbook.safezip(x, xerr[1])]
2901-
else:
2902-
# Check if xerr is scalar or symmetric. Asymmetric is handled
2903-
# above. This prevents Nx2 arrays from accidentally
2904-
# being accepted, when the user meant the 2xN transpose.
2905-
# special case for empty lists
2906-
if len(xerr) > 1 and not ((len(xerr) == len(x) and not (
2907-
iterable(xerr[0]) and len(xerr[0]) > 1))):
2908-
raise ValueError("xerr must be a scalar, the same "
2894+
def extract_err(err, data):
2895+
'''private function to compute error bars
2896+
2897+
Parameters
2898+
----------
2899+
err : iterable
2900+
xerr or yerr from errorbar
2901+
data : iterable
2902+
x or y from errorbar
2903+
'''
2904+
if (iterable(err) and len(err) == 2):
2905+
a, b = err
2906+
if iterable(a) and iterable(b):
2907+
# using list comps rather than arrays to preserve units
2908+
low = [thisx - thiserr for (thisx, thiserr)
2909+
in cbook.safezip(data, a)]
2910+
high = [thisx + thiserr for (thisx, thiserr)
2911+
in cbook.safezip(data, b)]
2912+
return low, high
2913+
# Check if xerr is scalar or symmetric. Asymmetric is handled
2914+
# above. This prevents Nx2 arrays from accidentally
2915+
# being accepted, when the user meant the 2xN transpose.
2916+
# special case for empty lists
2917+
if len(err) > 1:
2918+
fe = safe_first_element(err)
2919+
if not ((len(err) == len(data) and not (iterable(fe) and
2920+
len(fe) > 1))):
2921+
raise ValueError("err must be a scalar, the same "
29092922
"dimensions as x, or 2xN.")
2910-
# using list comps rather than arrays to preserve units
2911-
left = [thisx - thiserr for (thisx, thiserr)
2912-
in cbook.safezip(x, xerr)]
2913-
right = [thisx + thiserr for (thisx, thiserr)
2914-
in cbook.safezip(x, xerr)]
2923+
# using list comps rather than arrays to preserve units
2924+
low = [thisx - thiserr for (thisx, thiserr)
2925+
in cbook.safezip(data, err)]
2926+
high = [thisx + thiserr for (thisx, thiserr)
2927+
in cbook.safezip(data, err)]
2928+
return low, high
29152929

2930+
if xerr is not None:
2931+
left, right = extract_err(xerr, x)
29162932
# select points without upper/lower limits in x and
29172933
# draw normal errorbars for these points
29182934
noxlims = ~(xlolims | xuplims)
@@ -2957,25 +2973,7 @@ def xywhere(xs, ys, mask):
29572973
caplines.extend(self.plot(xup, yup, 'k|', **plot_kw))
29582974

29592975
if yerr is not None:
2960-
if (iterable(yerr) and len(yerr) == 2 and
2961-
iterable(yerr[0]) and iterable(yerr[1])):
2962-
# using list comps rather than arrays to preserve units
2963-
lower = [thisy - thiserr for (thisy, thiserr)
2964-
in cbook.safezip(y, yerr[0])]
2965-
upper = [thisy + thiserr for (thisy, thiserr)
2966-
in cbook.safezip(y, yerr[1])]
2967-
else:
2968-
# Check for scalar or symmetric, as in xerr.
2969-
if len(yerr) > 1 and not ((len(yerr) == len(y) and not (
2970-
iterable(yerr[0]) and len(yerr[0]) > 1))):
2971-
raise ValueError("yerr must be a scalar, the same "
2972-
"dimensions as y, or 2xN.")
2973-
# using list comps rather than arrays to preserve units
2974-
lower = [thisy - thiserr for (thisy, thiserr)
2975-
in cbook.safezip(y, yerr)]
2976-
upper = [thisy + thiserr for (thisy, thiserr)
2977-
in cbook.safezip(y, yerr)]
2978-
2976+
lower, upper = extract_err(yerr, y)
29792977
# select points without upper/lower limits in y and
29802978
# draw normal errorbars for these points
29812979
noylims = ~(lolims | uplims)

0 commit comments

Comments
 (0)