Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2ecdc6b

Browse filesBrowse files
committed
DOC: MEP12 docstrings for errorbar examples
1 parent cf29664 commit 2ecdc6b
Copy full SHA for 2ecdc6b

File tree

Expand file treeCollapse file tree

4 files changed

+105
-52
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+105
-52
lines changed

‎examples/statistics/errorbar_demo.py

Copy file name to clipboard
+8-3Lines 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

Copy file name to clipboardExpand all lines: examples/statistics/errorbar_demo_features.py
+18-14Lines 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')
+43-20Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,73 @@
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

14-
fig = plt.figure()
15-
ax = fig.add_subplot(1, 1, 1)
30+
fig, ax = plt.subplots(figsize=(7, 4))
1631

1732
# standard error bars
18-
plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls)
33+
ax.errorbar(x, y, xerr=xerr, yerr=yerr, linestyle=ls)
1934

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

2539
# 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)
40+
ax.errorbar(x, y + 1.0, xerr=xerr, yerr=yerr, lolims=lolims,
41+
linestyle=ls)
2942

3043
# 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)
44+
ax.errorbar(x, y + 1.5, xerr=xerr, yerr=yerr,
45+
lolims=lolims, uplims=uplims,
46+
marker='o', markersize=8,
47+
linestyle=ls)
3348

34-
# including xlower and xupper limits
49+
# Plot a series with lower and upper limits in both x & y
50+
# constant x-error with varying y-error
3551
xerr = 0.2
3652
yerr = np.zeros(x.shape) + 0.2
3753
yerr[[3, 6]] = 0.3
54+
55+
# mock up some limits by modifying previous data
3856
xlolims = lolims
3957
xuplims = uplims
4058
lolims = np.zeros(x.shape)
4159
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')
60+
lolims[[6]] = True # only limited at this index
61+
uplims[[3]] = True # only limited at this index
62+
63+
# do the plotting
64+
ax.errorbar(x, y + 2.1, xerr=xerr, yerr=yerr,
65+
xlolims=xlolims, xuplims=xuplims,
66+
uplims=uplims, lolims=lolims,
67+
marker='o', markersize=8,
68+
linestyle='none')
4769

70+
# tidy up the figure
4871
ax.set_xlim((0, 5.5))
4972
ax.set_title('Errorbar upper and lower limits')
5073
plt.show()
+36-15Lines changed: 36 additions & 15 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,44 @@
1126
n = 5
1227

1328
# Dummy data
29+
np.random.seed(10)
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)
18-
xerr = np.random.rand(2, n)
19-
yerr = np.random.rand(2, n)
20-
21-
# Create figure and axes
22-
fig, ax = plt.subplots(1)
34+
xerr = np.random.rand(2, n) + 0.1
35+
yerr = np.random.rand(2, n) + 0.2
2336

2437

25-
def make_error_boxes(ax, xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5):
38+
def make_error_boxes(ax, xdata, ydata, xerror, yerror, facecolor='r',
39+
edgecolor='None', alpha=0.5):
2640

2741
# Create list for all the error patches
2842
errorboxes = []
2943

3044
# 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())
45+
for x, y, xe, ye in zip(xdata, ydata, xerror.T, yerror.T):
46+
rect = Rectangle((x - xe[0], y - ye[0]), xe.sum(), ye.sum())
3347
errorboxes.append(rect)
3448

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

3853
# Add collection to axes
3954
ax.add_collection(pc)
4055

4156
# Plot errorbars
42-
ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror, fmt='None', ecolor='k')
57+
artists = ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror,
58+
fmt='None', ecolor='k')
4359

60+
return artists
61+
62+
63+
# Create figure and axes
64+
fig, ax = plt.subplots(1)
4465

4566
# Call function to create error boxes
46-
make_error_boxes(ax, x, y, xerr, yerr)
67+
_ = make_error_boxes(ax, x, y, xerr, yerr)
4768

4869
plt.show()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.