8000 Merge pull request #6596 from tmdavison/master · matplotlib/matplotlib@9ea7315 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ea7315

Browse files
committed
Merge pull request #6596 from tmdavison/master
Added a new example to create error boxes using a PatchCollection
1 parent c88b341 commit 9ea7315

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
Example to create boxes from error using PatchCollection
3+
'''
4+
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
from matplotlib.collections import PatchCollection
8+
from matplotlib.patches import Rectangle
9+
10+
# Number of data points
11+
n = 5
12+
13+
# Dummy data
14+
x = np.arange(0, n, 1)
15+
y = np.random.rand(n)*5.
16+
17+
# 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)
23+
24+
25+
def make_error_boxes(ax, xdata, ydata, xerror, yerror, fc='r', ec='None', alpha=0.5):
26+
27+
# Create list for all the error patches
28+
errorboxes = []
29+
30+
# 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())
33+
errorboxes.append(rect)
34+
35+
# Create patch collection with specified colour/alpha
36+
pc = PatchCollection(errorboxes, facecolor=fc, alpha=alpha, edgecolor=ec)
37+
38+
# Add collection to axes
39+
ax.add_collection(pc)
40+
41+
# Plot errorbars
42+
ax.errorbar(xdata, ydata, xerr=xerror, yerr=yerror, fmt='None', ecolor='k')
43+
44+
45+
# Call function to create error boxes
46+
make_error_boxes(ax, x, y, xerr, yerr)
47+
48+
plt.show()

0 commit comments

Comments
 (0)
0