8000 Merge pull request #5593 from tacaswell/fix_errorbar_cycleing · matplotlib/matplotlib@acfab37 · GitHub
[go: up one dir, main page]

Skip to content

Commit acfab37

Browse files
committed
Merge pull request #5593 from tacaswell/fix_errorbar_cycleing
ENH: errorbar color cycle clean up
1 parent 8fcc26e commit acfab37

File tree

1 file changed

+113
-95
lines changed

1 file changed

+113
-95
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 113 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,8 @@ def errorbar(self, x, y, yerr=None, xerr=None,
28292829
.. plot:: mpl_examples/statistics/errorbar_demo.py
28302830
28312831
"""
2832+
kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
2833+
kwargs.setdefault('zorder', 2)
28322834

28332835
if errorevery < 1:
28342836
raise ValueError(
@@ -2842,15 +2844,32 @@ def errorbar(self, x, y, yerr=None, xerr=None,
28422844

28432845
if fmt is None:
28442846
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.')
28482850
warnings.warn(msg, mplDeprecation, stacklevel=1)
28492851

28502852
plot_line = (fmt.lower() != 'none')
2851-
28522853
label = kwargs.pop("label", None)
28532854

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']
28542873
# make sure all the args are iterable; use lists not arrays to
28552874
# preserve units
28562875
if not iterable(x):
@@ -2867,47 +2886,69 @@ def errorbar(self, x, y, yerr=None, xerr=None,
28672886
if not iterable(yerr):
28682887
yerr = [yerr] * len(y)
28692888

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
28712896

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
28762902

2877-
barcols = []
2878-
caplines = []
2879-
2880-
lines_kw = {'label': '_nolegend_'}
28812903
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+
28872908
for key in ('transform', 'alpha', 'zorder', 'rasterized'):
28882909
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
28902924

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
28962932

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)
29012937

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 = []
29062940

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))
29112952

29122953
everymask = np.arange(len(x)) % errorevery == 0
29132954

@@ -2922,25 +2963,6 @@ def xywhere(xs, ys, mask):
29222963
ys = [thisy for thisy, b in zip(ys, mask) if b]
29232964
return xs, ys
29242965

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-
29442966
def extract_err(err, data):
29452967
'''private function to compute error bars
29462968
@@ -2985,42 +3007,46 @@ def extract_err(err, data):
29853007
if noxlims.any():
29863008
yo, _ = xywhere(y, right, noxlims & everymask)
29873009
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))
29893011
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))
29923016

29933017
if xlolims.any():
29943018
yo, _ = xywhere(y, right, xlolims & everymask)
29953019
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))
29973021
rightup, yup = xywhere(right, y, xlolims & everymask)
29983022
if self.xaxis_inverted():
29993023
marker = mlines.CARETLEFTBASE
30003024
else:
30013025
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))
30053029
if capsize > 0:
30063030
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))
30083033

30093034
if xuplims.any():
30103035
yo, _ = xywhere(y, right, xuplims & everymask)
30113036
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))
30133038
leftlo, ylo = xywhere(left, y, xuplims & everymask)
30143039
if self.xaxis_inverted():
30153040
marker = mlines.CARETRIGHTBASE
30163041
else:
30173042
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))
30213046
if capsize > 0:
30223047
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))
30243050

30253051
if yerr is not None:
30263052
lower, upper = extract_err(yerr, y)
@@ -3030,61 +3056,53 @@ def extract_err(err, data):
30303056
if noylims.any():
30313057
xo, _ = xywhere(x, lower, noylims & everymask)
30323058
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))
30343060
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))
30373065

30383066
if lolims.any():
30393067
xo, _ = xywhere(x, lower, lolims & everymask)
30403068
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))
30423070
xup, upperup = xywhere(x, upper, lolims & everymask)
30433071
if self.yaxis_inverted():
30443072
marker = mlines.CARETDOWNBASE
30453073
else:
30463074
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))
30503078
if capsize > 0:
30513079
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))
30533082

30543083
if uplims.any():
30553084
xo, _ = xywhere(x, lower, uplims & everymask)
30563085
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))
30583087
xlo, lowerlo = xywhere(x, lower, uplims & everymask)
30593088
if self.yaxis_inverted():
30603089
marker = mlines.CARETUPBASE
30613090
else:
30623091
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))
30663095
if capsize > 0:
30673096
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))
30813099
for l in caplines:
3082-
l.set_color(ecolor)
3100+
self.add_line(l)
30833101

30843102
self.autoscale_view()
30853103
self._hold = holdstate
30863104

3087-
errorbar_container = ErrorbarContainer((l0, tuple(caplines),
3105+
errorbar_container = ErrorbarContainer((data_line, tuple(caplines),
30883106
tuple(barcols)),
30893107
has_xerr=(xerr is not None),
30903108
has_yerr=(yerr is not None),

0 commit comments

Comments
 (0)
0