@@ -2829,6 +2829,8 @@ def errorbar(self, x, y, yerr=None, xerr=None,
2829
2829
.. plot:: mpl_examples/statistics/errorbar_demo.py
2830
2830
2831
2831
"""
2832
+ kwargs = cbook .normalize_kwargs (kwargs , _alias_map )
2833
+ kwargs .setdefault ('zorder' , 2 )
2832
2834
2833
2835
if errorevery < 1 :
2834
2836
raise ValueError (
@@ -2842,15 +2844,32 @@ def errorbar(self, x, y, yerr=None, xerr=None,
2842
2844
2843
2845
if fmt is None :
2844
2846
fmt = 'none'
2845
- msg = ('Use of None object as fmt keyword argument to '
2846
- + 'suppress plotting of data values is deprecated '
2847
- + 'since 1.4; use the string "none" instead.' )
2847
+ msg = ('Use of None object as fmt keyword argument to ' +
2848
+ 'suppress plotting of data values is deprecated ' +
2849
+ 'since 1.4; use the string "none" instead.' )
2848
2850
warnings .warn (msg , mplDeprecation , stacklevel = 1 )
2849
2851
2850
2852
plot_line = (fmt .lower () != 'none' )
2851
-
2852
2853
label = kwargs .pop ("label" , None )
2853
2854
2855
+ fmt_style_kwargs = {k : v for k , v in
2856
+ zip (('linestyle' , 'marker' , 'color' ),
2857
+ _process_plot_format (fmt )) if v is not None }
2858
+
2859
+ if ('color' in kwargs or 'color' in fmt_style_kwargs or
2860
+ ecolor is not None ):
2861
+ base_style = {}
2862
+ if 'color' in kwargs :
2863
+ base_style ['color' ] = kwargs .pop ('color' )
2864
+ else :
2865
+ base_style = six .next (self ._get_lines .prop_cycler )
2866
+
2867
+ base_style ['label' ] = '_nolegend_'
2868
+ base_style .update (fmt_style_kwargs )
2869
+ if 'color' not in base_style :
2870
+ base_style ['color' ] = 'C0'
2871
+ if ecolor is None :
2872
+ ecolor = base_style ['color' ]
2854
2873
# make sure all the args are iterable; use lists not arrays to
2855
2874
# preserve units
2856
2875
if not iterable (x ):
@@ -2867,47 +2886,69 @@ def errorbar(self, x, y, yerr=None, xerr=None,
2867
2886
if not iterable (yerr ):
2868
2887
yerr = [yerr ] * len (y )
2869
2888
2870
- l0 = None
2889
+ # make the style dict for the 'normal' plot line
2890
+ plot_line_style = dict (base_style )
2891
+ plot_line_style .update (** kwargs )
2892
+ if barsabove :
2893
+ plot_line_style ['zorder' ] = kwargs ['zorder' ] - .1
2894
+ else :
2895
+ plot_line_style ['zorder' ] = kwargs ['zorder' ] + .1
2871
2896
2872
- # Instead of using zorder, the line plot is being added
2873
- # either here, or after all the errorbar plot elements.
2874
- if barsabove and plot_line :
2875
- l0 , = self .plot (x , y , fmt , label = "_nolegend_" , ** kwargs )
2897
+ # make the style dict for the line collections (the bars)
2898
+ eb_lines_style = dict (base_style )
2899
+ eb_lines_style .pop ('marker' , None )
2900
+ eb_lines_style .pop ('linestyle' , None )
2901
+ eb_lines_style ['color' ] = ecolor
2876
2902
2877
- barcols = []
2878
- caplines = []
2879
-
2880
- lines_kw = {'label' : '_nolegend_' }
2881
2903
if elinewidth :
2882
- lines_kw ['linewidth' ] = elinewidth
2883
- else :
2884
- for key in ('linewidth' , 'lw' ):
2885
- if key in kwargs :
2886
- lines_kw [key ] = kwargs [key ]
2904
+ eb_lines_style ['linewidth' ] = elinewidth
2905
+ elif 'linewidth' in kwargs :
2906
+ eb_lines_style ['linewidth' ] = kwargs ['linewidth' ]
2907
+
2887
2908
for key in ('transform' , 'alpha' , 'zorder' , 'rasterized' ):
2888
2909
if key in kwargs :
2889
- lines_kw [key ] = kwargs [key ]
2910
+ eb_lines_style [key ] = kwargs [key ]
2911
+
2912
+ # set up cap style dictionary
2913
+ eb_cap_style = dict (base_style )
2914
+ # eject any marker information from format string
2915
+ eb_cap_style .pop ('marker' , None )
2916
+ eb_cap_style .pop ('ls' , None )
2917
+ eb_cap_style ['linestyle' ] = 'none'
2918
+ if capsize is None :
2919
+ capsize = rcParams ["errorbar.capsize" ]
2920
+ if capsize > 0 :
2921
+ eb_cap_style ['markersize' ] = 2. * capsize
2922
+ if capthick is not None :
2923
+ eb_cap_style ['markeredgewidth' ] = capthick
2890
2924
2891
- # arrays fine here, they are booleans and hence not units
2892
- if not iterable (lolims ):
2893
- lolims = np .asarray ([lolims ] * len (x ), bool )
2894
- else :
2895
- lolims = np .asarray (lolims , bool )
2925
+ # For backwards-compat, allow explicit setting of
2926
+ # 'markeredgewidth' to over-ride capthick.
2927
+ for key in ('markeredgewidth' , 'transform' , 'alpha' ,
2928
+ 'zorder' , 'rasterized' ):
2929
+ if key in kwargs :
2930
+ eb_cap_style [key ] = kwargs [key ]
2931
+ eb_cap_style ['color' ] = ecolor
2896
2932
2897
- if not iterable ( uplims ):
2898
- uplims = np . array ([ uplims ] * len ( x ), bool )
2899
- else :
2900
- uplims = np . asarray ( uplims , bool )
2933
+ data_line = None
2934
+ if plot_line :
2935
+ data_line = mlines . Line2D ( x , y , ** plot_line_style )
2936
+ self . add_line ( data_line )
2901
2937
2902
- if not iterable (xlolims ):
2903
- xlolims = np .array ([xlolims ] * len (x ), bool )
2904
- else :
2905
- xlolims = np .asarray (xlolims , bool )
2938
+ barcols = []
2939
+ caplines = []
2906
2940
2907
- if not iterable (xuplims ):
2908
- xuplims = np .array ([xuplims ] * len (x ), bool )
2909
- else :
2910
- xuplims = np .asarray (xuplims , bool )
2941
+ # arrays fine here, they are booleans and hence not units
2942
+ def _bool_asarray_helper (d , expected ):
2943
+ if not iterable (d ):
2944
+ return np .asarray ([d ] * expected , bool )
2945
+ else :
2946
+ return np .asarray (d , bool )
2947
+
2948
+ lolims = _bool_asarray_helper (lolims , len (x ))
2949
+ uplims = _bool_asarray_helper (uplims , len (x ))
2950
+ xlolims = _bool_asarray_helper (xlolims , len (x ))
2951
+ xuplims = _bool_asarray_helper (xuplims , len (x ))
2911
2952
2912
2953
everymask = np .arange (len (x )) % errorevery == 0
2913
2954
@@ -2922,25 +2963,6 @@ def xywhere(xs, ys, mask):
2922
2963
ys = [thisy for thisy , b in zip (ys , mask ) if b ]
2923
2964
return xs , ys
2924
2965
2925
- plot_kw = {'label' : '_nolegend_' }
2926
- if capsize is None :
2927
- capsize = rcParams ["errorbar.capsize" ]
2928
- if capsize > 0 :
2929
- plot_kw ['ms' ] = 2. * capsize
2930
- if capthick is not None :
2931
- # 'mew' has higher priority, I believe,
2932
- # if both 'mew' and 'markeredgewidth' exists.
2933
- # So, save capthick to markeredgewidth so that
2934
- # explicitly setting mew or markeredgewidth will
2935
- # over-write capthick.
2936
- plot_kw ['markeredgewidth' ] = capthick
2937
- # For backwards-compat, allow explicit setting of
2938
- # 'mew' or 'markeredgewidth' to over-ride capthick.
2939
- for key in ('markeredgewidth' , 'mew' , 'transform' , 'alpha' ,
2940
- 'zorder' , 'rasterized' ):
2941
- if key in kwargs :
2942
- plot_kw [key ] = kwargs [key ]
2943
-
2944
2966
def extract_err (err , data ):
2945
2967
'''private function to compute error bars
2946
2968
@@ -2985,42 +3007,46 @@ def extract_err(err, data):
2985
3007
if noxlims .any ():
2986
3008
yo , _ = xywhere (y , right , noxlims & everymask )
2987
3009
lo , ro = xywhere (left , right , noxlims & everymask )
2988
- barcols .append (self .hlines (yo , lo , ro , ** lines_kw ))
3010
+ barcols .append (self .hlines (yo , lo , ro , ** eb_lines_style ))
2989
3011
if capsize > 0 :
2990
- caplines .extend (self .plot (lo , yo , 'k|' , ** plot_kw ))
2991
- caplines .extend (self .plot (ro , yo , 'k|' , ** plot_kw ))
3012
+ caplines .append (mlines .Line2D (lo , yo , marker = '|' ,
3013
+ ** eb_cap_style ))
3014
+ caplines .append (mlines .Line2D (ro , yo , marker = '|' ,
3015
+ ** eb_cap_style ))
2992
3016
2993
3017
if xlolims .any ():
2994
3018
yo , _ = xywhere (y , right , xlolims & everymask )
2995
3019
lo , ro = xywhere (x , right , xlolims & everymask )
2996
- barcols .append (self .hlines (yo , lo , ro , ** lines_kw ))
3020
+ barcols .append (self .hlines (yo , lo , ro , ** eb_lines_style ))
2997
3021
rightup , yup = xywhere (right , y , xlolims & everymask )
2998
3022
if self .xaxis_inverted ():
2999
3023
marker = mlines .CARETLEFTBASE
3000
3024
else :
3001
3025
marker = mlines .CARETRIGHTBASE
3002
- caplines .extend (
3003
- self . plot (rightup , yup , ls = 'None' , marker = marker ,
3004
- ** plot_kw ))
3026
+ caplines .append (
3027
+ mlines . Line2D (rightup , yup , ls = 'None' , marker = marker ,
3028
+ ** eb_cap_style ))
3005
3029
if capsize > 0 :
3006
3030
xlo , ylo = xywhere (x , y , xlolims & everymask )
3007
- caplines .extend (self .plot (xlo , ylo , 'k|' , ** plot_kw ))
3031
+ caplines .append (mlines .Line2D (xlo , ylo , marker = '|' ,
3032
+ ** eb_cap_style ))
3008
3033
3009
3034
if xuplims .any ():
3010
3035
yo , _ = xywhere (y , right , xuplims & everymask )
3011
3036
lo , ro = xywhere (left , x , xuplims & everymask )
3012
- barcols .append (self .hlines (yo , lo , ro , ** lines_kw ))<
1241
/div>
3037
+ barcols .append (self .hlines (yo , lo , ro , ** eb_lines_style ))
3013
3038
leftlo , ylo = xywhere (left , y , xuplims & everymask )
3014
3039
if self .xaxis_inverted ():
3015
3040
marker = mlines .CARETRIGHTBASE
3016
3041
else :
3017
3042
marker = mlines .CARETLEFTBASE
3018
- caplines .extend (
3019
- self . plot (leftlo , ylo , ls = 'None' , marker = marker ,
3020
- ** plot_kw ))
3043
+ caplines .append (
3044
+ mlines . Line2D (leftlo , ylo , ls = 'None' , marker = marker ,
3045
+ ** eb_cap_style ))
3021
3046
if capsize > 0 :
3022
3047
xup , yup = xywhere (x , y , xuplims & everymask )
3023
- caplines .extend (self .plot (xup , yup , 'k|' , ** plot_kw ))
3048
+ caplines .append (mlines .Line2D (xup , yup , marker = '|' ,
3049
+ ** eb_cap_style ))
3024
3050
3025
3051
if yerr is not None :
3026
3052
lower , upper = extract_err (yerr , y )
@@ -3030,61 +3056,53 @@ def extract_err(err, data):
3030
3056
if noylims .any ():
3031
3057
xo , _ = xywhere (x , lower , noylims & everymask )
3032
3058
lo , uo = xywhere (lower , upper , noylims & everymask )
3033
- barcols .append (self .vlines (xo , lo , uo , ** lines_kw ))
3059
+ barcols .append (self .vlines (xo , lo , uo , ** eb_lines_style ))
3034
3060
if capsize > 0 :
3035
- caplines .extend (self .plot (xo , lo , 'k_' , ** plot_kw ))
3036
- caplines .extend (self .plot (xo , uo , 'k_' , ** plot_kw ))
3061
+ caplines .append (mlines .Line2D (xo , lo , marker = '_' ,
3062
+ ** eb_cap_style ))
3063
+ caplines .append (mlines .Line2D (xo , uo , marker = '_' ,
3064
+ ** eb_cap_style ))
3037
3065
3038
3066
if lolims .any ():
3039
3067
xo , _ = xywhere (x , lower , lolims & everymask )
3040
3068
lo , uo = xywhere (y , upper , lolims & everymask )
3041
- barcols .append (self .vlines (xo , lo , uo , ** lines_kw ))
3069
+ barcols .append (self .vlines (xo , lo , uo , ** eb_lines_style ))
3042
3070
xup , upperup = xywhere (x , upper , lolims & everymask )
3043
3071
if self .yaxis_inverted ():
3044
3072
marker = mlines .CARETDOWNBASE
3045
3073
else :
3046
3074
marker = mlines.CARETUPBASE
3047
- caplines .extend (
3048
- self . plot (xup , upperup , ls = 'None' , marker = marker ,
3049
- ** plot_kw ))
3075
+ caplines .append (
3076
+ mlines . Line2D (xup , upperup , ls = 'None' , marker = marker ,
3077
+ ** eb_cap_style ))
3050
3078
if capsize > 0 :
3051
3079
xlo , ylo = xywhere (x , y , lolims & everymask )
3052
- caplines .extend (self .plot (xlo , ylo , 'k_' , ** plot_kw ))
3080
+ caplines .append (mlines .Line2D (xlo , ylo , marker = '_' ,
3081
+ ** eb_cap_style ))
3053
3082
3054
3083
if uplims .any ():
3055
3084
xo , _ = xywhere (x , lower , uplims & everymask )
3056
3085
lo , uo = xywhere (lower , y , uplims & everymask )
3057
- barcols .append (self .vlines (xo , lo , uo , ** lines_kw ))
3086
+ barcols .append (self .vlines (xo , lo , uo , ** eb_lines_style ))
3058
3087
xlo , lowerlo = xywhere (x , lower , uplims & everymask )
3059
3088
if self .yaxis_inverted ():
3060
3089
marker = mlines .CARETUPBASE
3061
3090
else :
3062
3091
marker = mlines .CARETDOWNBASE
3063
- caplines .extend (
3064
- self . plot (xlo , lowerlo , ls = 'None' , marker = marker ,
3065
- ** plot_kw ))
3092
+ caplines .append (
3093
+ mlines . Line2D (xlo , lowerlo , ls = 'None' , marker = marker ,
3094
+ ** eb_cap_style ))
3066
3095
if capsize > 0 :
3067
3096
xup , yup = xywhere (x , y , uplims & everymask )
3068
- caplines .extend (self .plot (xup , yup , 'k_' , ** plot_kw ))
3069
-
3070
- if not barsabove and plot_line :
3071
- l0 , = self .plot (x , y , fmt , label = '_nolegend_' , ** kwargs )
3072
-
3073
- if ecolor is None :
3074
- if l0 is None :
3075
- ecolor = self ._get_lines .get_next_color ()
3076
- else :
3077
- ecolor = l0 .get_color ()
3078
-
3079
- for l in barcols :
3080
- l .set_color (ecolor )
3097
+ caplines .append (mlines .Line2D (xup , yup , marker = '_' ,
3098
+ ** eb_cap_style ))
3081
3099
for l in caplines :
3082
- l . set_color ( ecolor )
3100
+ self . add_line ( l )
3083
3101
3084
3102
self .autoscale_view ()
3085
3103
self ._hold = holdstate
3086
3104
3087
- errorbar_container = ErrorbarContainer ((l0 , tuple (caplines ),
3105
+ errorbar_container = ErrorbarContainer ((data_line , tuple (caplines ),
3088
3106
tuple (barcols )),
3089
3107
has_xerr = (xerr is not None ),
3090
3108
has_yerr = (yerr is not None ),
0 commit comments