@@ -3108,41 +3108,27 @@ def xywhere(xs, ys, mask):
31083108return xs ,ys
31093109
31103110def extract_err (err ,data ):
3111- '''private function to compute error bars
3112-
3113- Parameters
3114- ----------
3115- err : iterable
3116- xerr or yerr from errorbar
3117- data : iterable
3118- x or y from errorbar
3119- '''
3120- try :
3111+ """
3112+ Private function to parse *err* and subtract/add it to *data*.
3113+
3114+ Both *err* and *data* are already iterables at this point.
3115+ """
3116+ try :# Asymmetric error: pair of 1D iterables.
31213117a ,b = err
3118+ iter (a )
3119+ iter (b )
31223120except (TypeError ,ValueError ):
3123- pass
3124- else :
3125- if np .iterable (a )and np .iterable (b ):
3126- # using list comps rather than arrays to preserve units
3127- low = [thisx - thiserr for thisx ,thiserr
3128- in cbook .safezip (data ,a )]
3129- high = [thisx + thiserr for thisx ,thiserr
3130- in cbook .safezip (data ,b )]
3131- return low ,high
3132- # Check if xerr is scalar or symmetric. Asymmetric is handled
3133- # above. This prevents Nx2 arrays from accidentally
3134- # being accepted, when the user meant the 2xN transpose.
3135- # special case for empty lists
3136- if len (err )> 1 :
3137- fe = cbook .safe_first_element (err )
3138- if len (err )!= len (data )or np .size (fe )> 1 :
3139- raise ValueError ("err must be [ scalar | N, Nx1 "
3140- "or 2xN array-like ]" )
3141- # using list comps rather than arrays to preserve units
3142- low = [thisx - thiserr for thisx ,thiserr
3143- in cbook .safezip (data ,err )]
3144- high = [thisx + thiserr for thisx ,thiserr
3145- in cbook .safezip (data ,err )]
3121+ a = b = err # Symmetric error: 1D iterable.
3122+ # This could just be `np.ndim(a) > 1 and np.ndim(b) > 1`, except
3123+ # for the (undocumented, but tested) support for (n, 1) arrays.
3124+ ash ,bsh = map (np .shape , [a ,b ])
3125+ if (len (ash )> 2 and not (len (ash )== 2 and ash [1 ]== 1 )
3126+ or len (bsh )> 2 and not (len (bsh )== 2 and bsh [1 ]== 1 )):
3127+ raise ValueError (
3128+ "err must be a scalar or a 1D or (2, n) array-like" )
3129+ # Using list comprehensions rather than arrays to preserve units.
3130+ low = [v - e for v ,e in cbook .safezip (data ,a )]
3131+ high = [v + e for v ,e in cbook .safezip (data ,b )]
31463132return low ,high
31473133
31483134if xerr is not None :