Skip to content

Commit b97c8a3

Browse files
committed
DOC: docstrings for errorbar examples
1 parent d6542f8 commit b97c8a3

File tree

4 files changed

+102
-49
lines changed

4 files changed

+102
-49
lines changed

examples/statistics/errorbar_demo.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
"""Demo of the errorbar function.
2+
3+
This exhibits the most basic use of of the error bar method.
4+
In this case, constant values are provided for both the error
5+
in the x- and y-directions.
16
"""
2-
Demo of the errorbar function.
3-
"""
7+
48
import numpy as np
59
import matplotlib.pyplot as plt
610

711
# example data
812
x = np.arange(0.1, 4, 0.5)
913
y = np.exp(-x)
1014

11-
plt.errorbar(x, y, xerr=0.2, yerr=0.4)
15+
fig, ax = plt.subplots()
16+
ax.errorbar(x, y, xerr=0.2, yerr=0.4)
1217
plt.show()

examples/statistics/errorbar_demo_features.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
"""
2-
Demo of errorbar function with different ways of specifying error bars.
1+
"""Demo of the different ways of specifying error bars.
32
4-
Errors can be specified as a constant value (as shown in `errorbar_demo.py`),
5-
or as demonstrated in this example, they can be specified by an N x 1 or 2 x N,
6-
where N is the number of data points.
3+
Errors can be specified as a constant value (as shown in
4+
`errorbar_demo.py`), or as demonstrated in this example, they can be
5+
specified by an N x 1 or 2 x N, where N is the number of data points.
76
87
N x 1:
9-
Error varies for each point, but the error values are symmetric (i.e. the
10-
lower and upper values are equal).
8+
Error varies for each point, but the error values are
9+
symmetric (i.e. the lower and upper values are equal).
1110
1211
2 x N:
13-
Error varies for each point, and the lower and upper limits (in that order)
14-
are different (asymmetric case)
12+
Error varies for each point, and the lower and upper limits
13+
(in that order) are different (asymmetric case)
1514
16-
In addition, this example demonstrates how to use log scale with errorbar.
15+
In addition, this example demonstrates how to use log
16+
scale with error bars.
1717
"""
18+
1819
import numpy as np
1920
import matplotlib.pyplot as plt
2021

2122
# example data
2223
x = np.arange(0.1, 4, 0.5)
2324
y = np.exp(-x)
25+
2426
# example error bar values that vary with x-position
2527
error = 0.1 + 0.2 * x
26-
# error bar values w/ different -/+ errors
27-
lower_error = 0.4 * error
28-
upper_error = error
29-
asymmetric_error = [lower_error, upper_error]
3028

3129
fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)
3230
ax0.errorbar(x, y, yerr=error, fmt='-o')
3331
ax0.set_title('variable, symmetric error')
3432

33+
# error bar values w/ different -/+ errors that
34+
# also vary with the x-position
35+
lower_error = 0.4 * error
36+
upper_error = error
37+
asymmetric_error = [lower_error, upper_error]
38+
3539
ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o')
3640
ax1.set_title('variable, asymmetric error')
3741
ax1.set_yscale('log')
Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
1+
"""Demo of how to include upper and lower limits in error bars
2+
3+
In matplotlib, errors bars can have "limits". Applying limits to the
4+
error bars essentially makes the error unidirectional. Because of that,
5+
upper and lower limits can be applied in both the y- and x-directions
6+
via the `uplims`, `lolims`, `xuplims`, and `xlolims` parameters,
7+
respectively. This parameters can be scalar or boolean arrays.
8+
9+
For example, if `xlolims` is `True`, the x-error bars will only extend
10+
from the data towards increasing values. If `uplims` is an array filled
11+
with `False` except for the 3rd and 5th values, all of the y-error bars
12+
will be bi-directional, except the 3rd and 5th bars, which will extend
13+
from the data towards decreasing y-values.
114
"""
2-
Demo of the errorbar function, including upper and lower limits
3-
"""
15+
416
import numpy as np
517
import matplotlib.pyplot as plt
618

719
# example data
8-
x = np.arange(0.5, 5.5, 0.5)
20+
x = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0])
921
y = np.exp(-x)
1022
xerr = 0.1
1123
yerr = 0.2
24+
25+
# lower & upper limits of the error
26+
lolims = np.array([0, 0, 1, 0, 1, 0, 0, 0, 1, 0], dtype=bool)
27+
uplims = np.array([0, 1, 0, 0, 0, 1, 0, 0, 0, 1], dtype=bool)
1228
ls = 'dotted'
1329

1430
fig = plt.figure()
1531
ax = fig.add_subplot(1, 1, 1)
1632

1733
# standard error bars
18-
plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls)
34+
ax.errorbar(x, y, xerr=xerr, yerr=yerr, linestyle=ls)
1935

2036
# 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)
37+
ax.errorbar(x, y + 0.5, xerr=xerr, yerr=yerr, uplims=uplims,
38+
linestyle=ls)
2439

2540
# including lower limits
26-
lolims = np.zeros(x.shape)
27-
lolims[[2, 4, 8]] = True
28-
plt.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls)
41+
ax.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims,
42+
linestyle=ls)
2943

3044
# including upper and lower limits
31-
plt.errorbar(x, y + 1.5, marker='o', ms=8, xerr=xerr, yerr=yerr,
32-
lolims=lolims, uplims=uplims, ls=ls)
45+
ax.errorbar(x, y + 1.5, xerr=xerr, yerr=yerr,
46+
lolims=lolims, uplims=uplims,
47+
marker='o', markersize=8,
48+
linestyle=ls)
3349

34-
# including xlower and xupper limits
50+
## Plot a series with lower and upper limits in both x & y
51+
# constant x-error with varying y-error
3552
xerr = 0.2
3653
yerr = np.zeros(x.shape) + 0.2
3754
yerr[[3, 6]] = 0.3
55+
56+
# mock up some limits by modifying previous data
3857
xlolims = lolims
3958
xuplims = uplims
4059
lolims = np.zeros(x.shape)
4160
uplims = np.zeros(x.shape)
42-
lolims[[6]] = True
43-
uplims[[3]] = True
44-
plt.errorbar(x, y + 2.1, marker='o', ms=8, xerr=xerr, yerr=yerr,
45-
xlolims=xlolims, xuplims=xuplims, uplims=uplims, lolims=lolims,
46-
ls='none')
61+
lolims[[6]] = True # only limited at this index
62+
uplims[[3]] = True # only limited at this index
63+
64+
# do the plotting
65+
ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
66+
xlolims=xlolims, xuplims=xuplims,
67+
uplims=uplims, lolims=lolims,
68+
marker='o', markersize=8,
69+
linestyle='none')
4770

71+
# tidy up the figure
4872
ax.set_xlim((0, 5.5))
4973
ax.set_title('Errorbar upper and lower limits')
5074
plt.show()
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
'''
2-
Example to create boxes from error using PatchCollection
3-
'''
1+
"""Demo on creating boxes from error bars using PatchCollection
2+
3+
In this example, we snazz up a pretty standard error bar plot by adding
4+
a rectangle patch defined by the limits of the bars in both the x- and
5+
y- directions. Do this, we have to write our own custom function called
6+
`make_error_boxes`. Close inspection of this function will reveal the
7+
preferred pattern in writting functions for matplotlib:
8+
9+
1. an `Axes` object is passed directly to the function
10+
2. the function operates on the `Axes` methods directly, not through
11+
the `pyplot` interface
12+
3. plotting kwargs that could be abbreviated are spelled out for
13+
better code readability in the figure (for example we use
14+
`facecolor` instead of `fc`)
15+
4. the artists returned by the `Axes` plotting methods are then
16+
returned by the function so that, if desired, their styles
17+
can be modified later (they are not modified in this example).
18+
"""
419

520
import numpy as np
621
import matplotlib.pyplot as plt
@@ -11,38 +26,43 @@
1126
n = 5
1227

1328
# Dummy data
29+
np.random.seed(0)
1430
x = np.arange(0, n, 1)
15-
y = np.random.rand(n)*5.
31+
y = np.random.rand(n) * 5.
1632

1733
# Dummy errors (above and below)
1834
xerr = np.random.rand(2, n)
1935
yerr = np.random.rand(2, n)
2036

21-
# Create figure and axes
22-
fig, ax = plt.subplots(1)
23-
24-
25-
def make_error_boxes(ax, xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5):
37+
def make_error_boxes(ax, xdata, ydata, xerror, yerror, facecolor='r',
38+
edgecolor='None', alpha=0.5):
2639

2740
# Create list for all the error patches
2841
errorboxes = []
2942

3043
# Loop over data points; create box from errors at each point
31-
for xc, yc, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
32-
rect = Rectangle((xc-xe[0], yc-ye[0]), xe.sum(), ye.sum())
44+
for x, y, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
45+
rect = Rectangle((x - xe[0], y - ye[0]), xe.sum(), ye.sum())
3346
errorboxes.append(rect)
3447

3548
# Create patch collection with specified colour/alpha
36-
pc = PatchCollection(errorboxes, facecolor=fc, alpha=alpha, edgecolor=ec)
49+
pc = PatchCollection(errorboxes, facecolor=facecolor, alpha=alpha,
50+
edgecolor=edgecolor)
3751

3852
# Add collection to axes
3953
ax.add_collection(pc)
4054

4155
# Plot errorbars
42-
ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror, fmt='None', ecolor='k')
56+
artists = ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror,
57+
fmt='None', ecolor='k')
58+
59+
return artists
60+
4361

62+
# Create figure and axes
63+
fig, ax = plt.subplots(1)
4464

4565
# Call function to create error boxes
46-
make_error_boxes(ax, x, y, xerr, yerr)
66+
_ = make_error_boxes(ax, x, y, xerr, yerr)
4767

4868
plt.show()

0 commit comments

Comments
 (0)