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

Skip to content

Commit b76263c

Browse files
committed
Fixed issues with errorbar limits
1 parent 732f38a commit b76263c

File tree

2 files changed

+137
-83
lines changed

2 files changed

+137
-83
lines changed

examples/statistics/errorbar_lims.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Demo of the errorbar function, includig upper and lower limits
3+
"""
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
# example data
8+
x = np.arange(0.5, 5.5, 0.5)
9+
y = np.exp(-x)
10+
xerr = 0.1
11+
yerr = 0.2
12+
ls = 'dotted'
13+
14+
fig = plt.figure()
15+
ax = fig.add_subplot(1, 1, 1)
16+
17+
# standard error bars
18+
plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls, color='blue')
19+
20+
# including upper limits
21+
uplims = np.zeros(x.shape)
22+
uplims[[1, 5, 9]] = True
23+
plt.errorbar(x, y+0.5, xerr=xerr, yerr=yerr, uplims=uplims, ls=ls,
24+
color='green')
25+
26+
# including lower limits
27+
lolims = np.zeros(x.shape)
28+
lolims[[2, 4, 8]] = True
29+
plt.errorbar(x, y+1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls,
30+
color='red')
31+
32+
# including upper and lower limits
33+
plt.errorbar(x, y+1.5, marker='o', ms=8, xerr=xerr, yerr=yerr,
34+
lolims=lolims, uplims=uplims, ls=ls, color='magenta')
35+
36+
# including xlower and xupper limits
37+
xerr = 0.2
38+
yerr = np.zeros(x.shape) + 0.2
39+
yerr[[3, 6]] = 0.3
40+
xlolims = lolims
41+
xuplims = uplims
42+
lolims = np.zeros(x.shape)
43+
uplims = np.zeros(x.shape)
44+
lolims[[6]] = True
45+
uplims[[3]] = True
46+
plt.errorbar(x, y+2.1, marker='o', ms=8, xerr=xerr, yerr=yerr,
47+
xlolims=xlolims, xuplims=xuplims, uplims=uplims, lolims=lolims,
48+
ls='none', mec='blue', capsize=0, color='cyan')
49+
50+
ax.set_xlim((0, 5.5))
51+
plt.show()
52+

lib/matplotlib/axes/_axes.py

Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,29 +2727,28 @@ def xywhere(xs, ys, mask):
27272727
ys = [thisy for thisy, b in zip(ys, mask) if b]
27282728
return xs, ys
27292729

2730+
plot_kw = {'label': '_nolegend_'}
27302731
if capsize > 0:
2731-
plot_kw = {
2732-
'ms': 2 * capsize,
2733-
'label': '_nolegend_'}
2734-
if capthick is not None:
2735-
# 'mew' has higher priority, I believe,
2736-
# if both 'mew' and 'markeredgewidth' exists.
2737-
# So, save capthick to markeredgewidth so that
2738-
# explicitly setting mew or markeredgewidth will
2739-
# over-write capthick.
2740-
plot_kw['markeredgewidth'] = capthick
2741-
# For backwards-compat, allow explicit setting of
2742-
# 'mew' or 'markeredgewidth' to over-ride capthick.
2743-
if 'markeredgewidth' in kwargs:
2744-
plot_kw['markeredgewidth'] = kwargs['markeredgewidth']
2745-
if 'mew' in kwargs:
2746-
plot_kw['mew'] = kwargs['mew']
2747-
if 'transform' in kwargs:
2748-
plot_kw['transform'] = kwargs['transform']
2749-
if 'alpha' in kwargs:
2750-
plot_kw['alpha'] = kwargs['alpha']
2751-
if 'zorder' in kwargs:
2752-
plot_kw['zorder'] = kwargs['zorder']
2732+
plot_kw['ms'] = 2 * capsize
2733+
if capthick is not None:
2734+
# 'mew' has higher priority, I believe,
2735+
# if both 'mew' and 'markeredgewidth' exists.
2736+
# So, save capthick to markeredgewidth so that
2737+
# explicitly setting mew or markeredgewidth will
2738+
# over-write capthick.
2739+
plot_kw['markeredgewidth'] = capthick
2740+
# For backwards-compat, allow explicit setting of
2741+
# 'mew' or 'markeredgewidth' to over-ride capthick.
2742+
if 'markeredgewidth' in kwargs:
2743+
plot_kw['markeredgewidth'] = kwargs['markeredgewidth']
2744+
if 'mew' in kwargs:
2745+
plot_kw['mew'] = kwargs['mew']
2746+
if 'transform' in kwargs:
2747+
plot_kw['transform'] = kwargs['transform']
2748+
if 'alpha' in kwargs:
2749+
plot_kw['alpha'] = kwargs['alpha']
2750+
if 'zorder' in kwargs:
2751+
plot_kw['zorder'] = kwargs['zorder']
27532752

27542753
if xerr is not None:
27552754
if (iterable(xerr) and len(xerr) == 2 and
@@ -2766,38 +2765,38 @@ def xywhere(xs, ys, mask):
27662765
right = [thisx + thiserr for (thisx, thiserr)
27672766
in cbook.safezip(x, xerr)]
27682767

2769-
yo, _ = xywhere(y, right, everymask)
2770-
lo, ro = xywhere(left, right, everymask)
2771-
barcols.append(self.hlines(yo, lo, ro, **lines_kw))
2772-
if capsize > 0:
2773-
if xlolims.any():
2774-
# can't use numpy logical indexing since left and
2775-
# y are lists
2776-
leftlo, ylo = xywhere(left, y, xlolims & everymask)
2777-
2778-
caplines.extend(
2779-
self.plot(leftlo, ylo, ls='None',
2780-
marker=mlines.CARETLEFT, **plot_kw))
2781-
xlolims = ~xlolims
2782-
leftlo, ylo = xywhere(left, y, xlolims & everymask)
2783-
caplines.extend(self.plot(leftlo, ylo, 'k|', **plot_kw))
2784-
else:
2785-
2786-
leftlo, ylo = xywhere(left, y, everymask)
2787-
caplines.extend(self.plot(leftlo, ylo, 'k|', **plot_kw))
2788-
2789-
if xuplims.any():
2790-
2791-
rightup, yup = xywhere(right, y, xuplims & everymask)
2792-
caplines.extend(
2793-
self.plot(rightup, yup, ls='None',
2794-
marker=mlines.CARETRIGHT, **plot_kw))
2795-
xuplims = ~xuplims
2796-
rightup, yup = xywhere(right, y, xuplims & everymask)
2797-
caplines.extend(self.plot(rightup, yup, 'k|', **plot_kw))
2798-
else:
2799-
rightup, yup = xywhere(right, y, everymask)
2800-
caplines.extend(self.plot(rightup, yup, 'k|', **plot_kw))
2768+
noxlims = ~(xlolims | xuplims)
2769+
if noxlims.any():
2770+
yo, _ = xywhere(y, right, noxlims & everymask)
2771+
lo, ro = xywhere(left, right, noxlims & everymask)
2772+
barcols.append(self.hlines(yo, lo, ro, **lines_kw))
2773+
if capsize > 0:
2774+
caplines.extend(self.plot(lo, yo, 'k|', **plot_kw))
2775+
caplines.extend(self.plot(ro, yo, 'k|', **plot_kw))
2776+
2777+
if xlolims.any():
2778+
yo, _ = xywhere(y, right, xlolims & everymask)
2779+
lo, ro = xywhere(x, right, xlolims & everymask)
2780+
barcols.append(self.hlines(yo, lo, ro, **lines_kw))
2781+
rightup, yup = xywhere(right, y, xlolims & everymask)
2782+
caplines.extend(
2783+
self.plot(rightup, yup, ls='None',
2784+
marker=mlines.CARETRIGHT, **plot_kw))
2785+
if capsize > 0:
2786+
xlo, ylo = xywhere(x, y, xlolims & everymask)
2787+
caplines.extend(self.plot(xlo, ylo, 'k|', **plot_kw))
2788+
2789+
if xuplims.any():
2790+
yo, _ = xywhere(y, right, xuplims & everymask)
2791+
lo, ro = xywhere(left, x, xuplims & everymask)
2792+
barcols.append(self.hlines(yo, lo, ro, **lines_kw))
2793+
leftlo, ylo = xywhere(left, y, xuplims & everymask)
2794+
caplines.extend(
2795+
self.plot(leftlo, ylo, ls='None',
2796+
marker=mlines.CARETLEFT, **plot_kw))
2797+
if capsize > 0:
2798+
xup, yup = xywhere(x, y, xuplims & everymask)
2799+
caplines.extend(self.plot(xup, yup, 'k|', **plot_kw))
28012800

28022801
if yerr is not None:
28032802
if (iterable(yerr) and len(yerr) == 2 and
@@ -2814,35 +2813,38 @@ def xywhere(xs, ys, mask):
28142813
upper = [thisy + thiserr for (thisy, thiserr)
28152814
in cbook.safezip(y, yerr)]
28162815

2817-
xo, _ = xywhere(x, lower, everymask)
2818-
lo, uo = xywhere(lower, upper, everymask)
2819-
barcols.append(self.vlines(xo, lo, uo, **lines_kw))
2820-
if capsize > 0:
2821-
2822-
if lolims.any():
2823-
xlo, lowerlo = xywhere(x, lower, lolims & everymask)
2824-
caplines.extend(
2825-
self.plot(xlo, lowerlo, ls='None',
2826-
marker=mlines.CARETDOWN, **plot_kw))
2827-
lolims = ~lolims
2828-
xlo, lowerlo = xywhere(x, lower, lolims & everymask)
2829-
caplines.extend(self.plot(xlo, lowerlo, 'k_', **plot_kw))
2830-
else:
2831-
xlo, lowerlo = xywhere(x, lower, everymask)
2832-
caplines.extend(self.plot(xlo, lowerlo, 'k_', **plot_kw))
2833-
2834-
if uplims.any():
2835-
xup, upperup = xywhere(x, upper, uplims & everymask)
2836-
2837-
caplines.extend(
2838-
self.plot(xup, upperup, ls='None',
2839-
marker=mlines.CARETUP, **plot_kw))
2840-
uplims = ~uplims
2841-
xup, upperup = xywhere(x, upper, uplims & everymask)
2842-
caplines.extend(self.plot(xup, upperup, 'k_', **plot_kw))
2843-
else:
2844-
xup, upperup = xywhere(x, upper, everymask)
2845-
caplines.extend(self.plot(xup, upperup, 'k_', **plot_kw))
2816+
noylims = ~(lolims | uplims)
2817+
if noylims.any():
2818+
xo, _ = xywhere(x, lower, noylims & everymask)
2819+
lo, uo = xywhere(lower, upper, noylims & everymask)
2820+
barcols.append(self.vlines(xo, lo, uo, **lines_kw))
2821+
if capsize > 0:
2822+
caplines.extend(self.plot(xo, lo, 'k_', **plot_kw))
2823+
caplines.extend(self.plot(xo, uo, 'k_', **plot_kw))
2824+
2825+
if lolims.any():
2826+
xo, _ = xywhere(x, lower, lolims & everymask)
2827+
lo, uo = xywhere(y, upper, lolims & everymask)
2828+
barcols.append(self.vlines(xo, lo, uo, **lines_kw))
2829+
xup, upperup = xywhere(x, upper, lolims & everymask)
2830+
caplines.extend(
2831+
self.plot(xup, upperup, ls='None',
2832+
marker=mlines.CARETUP, **plot_kw))
2833+
if capsize > 0:
2834+
xlo, ylo = xywhere(x, y, lolims & everymask)
2835+
caplines.extend(self.plot(xlo, ylo, 'k_', **plot_kw))
2836+
2837+
if uplims.any():
2838+
xo, _ = xywhere(x, lower, uplims & everymask)
2839+
lo, uo = xywhere(lower, y, uplims & everymask)
2840+
barcols.append(self.vlines(xo, lo, uo, **lines_kw))
2841+
xlo, lowerlo = xywhere(x, lower, uplims & everymask)
2842+
caplines.extend(
2843+
self.plot(xlo, lowerlo, ls='None',
2844+
marker=mlines.CARETDOWN, **plot_kw))
2845+
if capsize > 0:
2846+
xup, yup = xywhere(x, y, uplims & everymask)
2847+
caplines.extend(self.plot(xup, yup, 'k_', **plot_kw))
28462848

28472849
if not barsabove and fmt is not None:
28482850
l0, = self.plot(x, y, fmt, **kwargs)

0 commit comments

Comments
 (0)