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