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

Skip to content

Commit cd778db

Browse files
committed
Added a new example to create error boxes using a PatchCollection
1 parent af77c85 commit cd778db

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+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from matplotlib.collections import PatchCollection
4+
from matplotlib.patches import Rectangle
5+
6+
# Number of data points
7+
n=5
8+
9+
# Dummy data
10+
x=np.arange(0,n,1)
11+
y=np.random.rand(n)*5.
12+
13+
# Dummy errors (above and below)
14+
xerr=np.random.rand(2,n)
15+
yerr=np.random.rand(2,n)
16+
17+
print xerr[:,0]
18+
19+
# Create figure and axes
20+
fig,ax = plt.subplots(1)
21+
22+
# Plot data points
23+
ax.errorbar(x,y,xerr=xerr,yerr=yerr,fmt='None',ecolor='k')
24+
25+
# Function to plot error boxes
26+
def makeErrorBoxes(xdata,ydata,xerror,yerror,fc='r',ec='None',alpha=0.5):
27+
28+
# Create list for all the error patches
29+
errorboxes = []
30+
31+
# Loop over data points; create box from errors at each point
32+
for xc,yc,xe,ye in zip(xdata,ydata,xerror.T,yerror.T):
33+
rect = Rectangle((xc-xe[0],yc-ye[0]),xe.sum(),ye.sum())
34+
errorboxes.append(rect)
35+
36+
# Create patch collection with specified colour/alpha
37+
pc = PatchCollection(errorboxes,facecolor=fc,alpha=alpha,edgecolor=ec)
38+
39+
# Add collection to axes
40+
ax.add_collection(pc)
41+
42+
# Call function to create error boxes
43+
makeErrorBoxes(x,y,xerr,yerr)
44+
45+
# Add some space around the data points on the axes
46+
ax.margins(0.1)
47+
48+
plt.show()

0 commit comments

Comments
 (0)