1 """This makes a little 'star' of error bars, using pylab/matplotlib.
2 """
3
4 import math
5
6 import pylab
7 import matplotlib
8
9 Grey = (0.7,0.7,0.7)
10
12 """This is a class that makes error bars, tilted at various angles."""
13
14 - def __init__(self, cx, cy, n, **kwargs):
15 self.cx = cx
16 self.cy = cy
17 self.n = n
18 self.kwargs = {'marker': 'o', 'linestyle': '-',
19 'mec': 'r', 'color': 'k', 'zorder': 2
20 }
21 self.kwargs.update(kwargs)
22
23
24 - def make(self, i, xl, xc, xh, **kwargs):
25 kwa = self.kwargs.copy()
26 kwa.update(kwargs)
27 assert xl <= xc <= xh
28 theta = (math.pi/2.0) * (1 + (i-0.5*(self.n-1))/float(self.n))
29 W = 0.12*(abs(xl)+abs(xh))/float(self.n)
30 w = kwa.get('w', W)
31 x1, y1 = self._xf(theta, (xl, 0.0), (xh, 0.0))
32 x2, y2 = self._xf(theta, (xl, -w), (xl, w))
33 x3, y3 = self._xf(theta, (xh, -w), (xh, w))
34 if (xl>0)==(xh>0):
35 zo = 2
36 else:
37 mcolor = kwa.get('mucolor', Grey)
38 color = kwa.get('lucolor', Grey)
39 zo = 1
40 zorder = kwa['zorder']
41 mcolor = kwa['mec']
42 color = kwa['color']
43 linestyle = kwa['linestyle']
44 pylab.plot(x1, y1, linestyle=linestyle, color=color, zorder=zorder)
45 pylab.plot(x2, y2, linestyle=linestyle, color=color, zorder=zorder)
46 pylab.plot(x3, y3, linestyle=linestyle, color=color, zorder=zorder)
47 if min(xl, xh) > 0.0:
48 x5, y5 = self._xf(theta, (0.0, 0.0), (min(xl, xh), 0.0))
49 pylab.plot(x5, y5, linestyle=':', color=color, zorder=100)
50 if max(xl, xh) < 0.0:
51 x5, y5 = self._xf(theta, (0.0, 0.0), (max(xl, xh), 0.0))
52 pylab.plot(x5, y5, linestyle=':', color=color, zorder=100)
53 x4, y4 = self._xf(theta, (xc, 0))
54 pylab.plot(x4, y4, marker=kwa['marker'], linestyle='', mec=mcolor, mew=0.0,
55 color=mcolor, zorder=zorder)
56
57
58 - def _xf(self, theta, *xy):
59 ox = []
60 oy = []
61 for (x,y) in xy:
62 xx = self.cx + math.cos(theta)*x - math.sin(theta)*y
63 yy = self.cy + math.sin(theta)*x + math.cos(theta)*y
64 ox.append(xx)
65 oy.append(yy)
66 return (ox, oy)
67
68
69
71 r, g, b = matplotlib.colors.colorConverter.to_rgb(c)
72 return (0.2*r+0.6, 0.2*g+0.6, 0.2*b+0.6)
73
74
75
77 - def __init__(self, l, c, h, dim=False, sortorder=None, **kwargs):
78 self.l = l
79 self.c = c
80 self.h = h
81 self.dim = dim
82 self.order = sortorder
83 self.kwargs = kwargs
84
86 return errorbar(self.l*other, self.c*other, self.h*other, self.dim, **(self.kwargs))
87
89 return cmp(self.order, other.order)
90
91
92 -def star(cx, cy, lch, lcolormap=None, mcolormap=None, dimmer=grey_out, **kwargs):
93 """This makes a star of error bars.
94 @param cx: x-coordinate of the star center
95 @param cy: y-coordinate of the star center
96 @param lch: list of error bars to create about the center.
97 @type lch: list(errorbar).
98 """
99 e = errorbar_maker(cx, cy, len(lch), **kwargs)
100 for (i, ebar) in enumerate(lch):
101 kwa = {}
102 if lcolormap is not None:
103 kwa['color'] = lcolormap[i]
104 elif mcolormap is not None:
105 kwa['color'] = mcolormap[i]
106 if mcolormap is not None:
107 kwa['mec'] = mcolormap[i]
108 elif lcolormap is not None:
109 kwa['mec'] = lcolormap[i]
110 kwa.update(ebar.kwargs)
111 if ebar.dim:
112 kwa['color'] = dimmer(kwa['color'])
113 kwa['mec'] = dimmer(kwa['mec'])
114 kwa['zorder'] = 1
115 e.make(i, ebar.l, ebar.c, ebar.h, **kwa)
116
117
118
119 -def plot(xlch, **kwargs):
120 assert xlch
121 sz = 0.0
122 n = 0
123 dx = 0.0
124 xlast = None
125 for (x, llch) in xlch:
126 for lch in llch:
127 if lch is not None:
128 sz += max(abs(lch.l), abs(lch.h))**2
129 n += 1
130 if xlast is not None:
131 dx += abs(x-xlast)
132 xlast = x
133 if n == 0:
134 return
135 f = 0.5*(dx/(len(xlch)-1))/math.sqrt(sz/float(n))
136 for (x, llch) in xlch:
137 star(x, 0.0,
138 [lch*f for lch in llch if lch is not None ],
139 **kwargs)
140
141
142 if __name__ == '__main__':
143 plot( [(0.0, [errorbar(0, 1, 2), errorbar(0, 2, 3), errorbar(0, 2, 3), errorbar(0, 1, 2)]),
144 (1.0, [errorbar(0, 1, 2), errorbar(1, 2, 3), errorbar(1, 2, 3), errorbar(0, 1, 2)]),
145 (2.0, [errorbar(-1, 1, 2, dim=True), errorbar(-1, 0, 1, dim=True), errorbar(-1, -0.5, 0), errorbar(-1, -0.5, -0.25)])
146 ],
147 lcolormap = ['r', 'g', 'b', 'c', 'm', 'k']
148 )
149 pylab.show()
150