1 | #!/usr/bin/python
|
---|
2 | #
|
---|
3 | # Werner Lustermann, Dominik Neise
|
---|
4 | # ETH Zurich, TU Dortmund
|
---|
5 | #
|
---|
6 | # plotter.py
|
---|
7 |
|
---|
8 | import numpy as np
|
---|
9 | import matplotlib.pyplot as plt
|
---|
10 |
|
---|
11 | class Plotter(object):
|
---|
12 | """ simple x-y plot """
|
---|
13 | def __init__(self, name, x=None, style = '.:', xlabel='x', ylabel='y', ion=True, grid=True, fname=None):
|
---|
14 | """ initialize the object """
|
---|
15 |
|
---|
16 | self.name = name
|
---|
17 | self.x = x
|
---|
18 | self.style = style
|
---|
19 | self.xlabel = xlabel
|
---|
20 | self.ylabel = ylabel
|
---|
21 |
|
---|
22 | #not sure if this should go here
|
---|
23 | if ion:
|
---|
24 | plt.ion()
|
---|
25 |
|
---|
26 | self.figure = plt.figure()
|
---|
27 | self.fig_id = self.figure.number
|
---|
28 |
|
---|
29 | plt.grid(grid)
|
---|
30 | self.fname = fname
|
---|
31 |
|
---|
32 | def __call__(self, ydata, label=None):
|
---|
33 | """ set ydata of plot """
|
---|
34 | style = self.style
|
---|
35 |
|
---|
36 | # make acitve and clear
|
---|
37 | plt.figure(self.fig_id)
|
---|
38 | plt.cla()
|
---|
39 |
|
---|
40 | # the following if else stuff is horrible,
|
---|
41 | # but I want all those possibilities, .... still working on it.
|
---|
42 |
|
---|
43 | # check if 1Dim oder 2Dim
|
---|
44 | ydata = np.array(ydata)
|
---|
45 | if ydata.ndim ==1:
|
---|
46 | if self.x==None:
|
---|
47 | plt.plot(ydata, self.style, label=label)
|
---|
48 | else:
|
---|
49 | plt.plot(self.x, ydata, self.style, label=label)
|
---|
50 | else:
|
---|
51 | for i in range(len(ydata)):
|
---|
52 | if self.x==None:
|
---|
53 | if label:
|
---|
54 | plt.plot(ydata[i], style, label=label[i])
|
---|
55 | else:
|
---|
56 | plt.plot(ydata[i], style)
|
---|
57 | else:
|
---|
58 | if label:
|
---|
59 | plt.plot(self.x, ydata[i], style, label=label[i])
|
---|
60 | else:
|
---|
61 | plt.plot(self.x, ydata[i], style)
|
---|
62 | plt.title(self.name)
|
---|
63 | plt.xlabel(self.xlabel)
|
---|
64 | plt.ylabel(self.ylabel)
|
---|
65 | if label:
|
---|
66 | plt.legend()
|
---|
67 |
|
---|
68 | if self.fname != None:
|
---|
69 | plt.savefig(self.fname)
|
---|
70 |
|
---|
71 | plt.draw()
|
---|
72 |
|
---|
73 |
|
---|
74 | class CamPlotter(object):
|
---|
75 | """ plotting data color-coded into FACT-camera """
|
---|
76 | def __init__(self, name, ion=True, grid=True, fname=None, map_file_path = '../map_dn.txt', vmin=None, vmax=None):
|
---|
77 | """ initialize the object """
|
---|
78 | self.name = name
|
---|
79 |
|
---|
80 | if ion:
|
---|
81 | plt.ion()
|
---|
82 |
|
---|
83 | chid, y,x,xe,ye,yh,xh,softid,hardid = np.loadtxt(map_file_path ,unpack=True)
|
---|
84 | self.xe = xe
|
---|
85 | self.ye = ye
|
---|
86 |
|
---|
87 | self.H = (6,0,30./180.*3.1415926)
|
---|
88 |
|
---|
89 | self.figure = plt.figure(figsize=(6, 6), dpi=80)
|
---|
90 | self.fig_id = self.figure.number
|
---|
91 |
|
---|
92 | self.grid = grid
|
---|
93 | self.fname = fname
|
---|
94 | self.vmin = vmin
|
---|
95 | self.vmax = vmax
|
---|
96 |
|
---|
97 | def __call__(self, data):
|
---|
98 | xe = self.xe
|
---|
99 | ye = self.ye
|
---|
100 | H = self.H
|
---|
101 | name = self.name
|
---|
102 | grid = self.grid
|
---|
103 | vmin = self.vmin
|
---|
104 | vmax = self.vmax
|
---|
105 |
|
---|
106 | plt.figure(self.fig_id)
|
---|
107 | plt.clf()
|
---|
108 | self.ax = self.figure.add_subplot(111, aspect='equal')
|
---|
109 | self.ax.axis([-22,22,-22,22])
|
---|
110 | self.ax.set_title(name)
|
---|
111 | self.ax.grid(grid)
|
---|
112 |
|
---|
113 | result = self.ax.scatter(xe,ye,s=25,alpha=1, c=data, marker=H, linewidths=0., vmin=vmin, vmax=vmax)
|
---|
114 | self.figure.colorbar( result, shrink=0.8, pad=-0.04 )
|
---|
115 |
|
---|
116 | plt.draw()
|
---|
117 |
|
---|
118 | class HistPlotter(object):
|
---|
119 |
|
---|
120 | def __init__(self, name, bins, range, grid=True, ion=True):
|
---|
121 | """ initialize the object """
|
---|
122 | self.bins = bins
|
---|
123 | self.range = range
|
---|
124 | self.name = name
|
---|
125 | self.figure = plt.figure()
|
---|
126 | self.fig_id = self.figure.number
|
---|
127 | self.grid = grid
|
---|
128 |
|
---|
129 | if ion:
|
---|
130 | plt.ion()
|
---|
131 |
|
---|
132 | def __call__(self, ydata, label=None, log=False):
|
---|
133 | plt.figure(self.fig_id)
|
---|
134 | plt.cla()
|
---|
135 |
|
---|
136 | bins = self.bins
|
---|
137 | range = self.range
|
---|
138 | grid = self.grid
|
---|
139 |
|
---|
140 | ydata = np.array(ydata)
|
---|
141 |
|
---|
142 | if ydata.ndim > 1:
|
---|
143 | ydata = ydata.flatten()
|
---|
144 |
|
---|
145 | plt.hist(ydata, bins, range, label=label, log=log)
|
---|
146 | if label:
|
---|
147 | plt.legend()
|
---|
148 | plt.title(self.name)
|
---|
149 |
|
---|
150 | plt.draw()
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 | def _test_Plotter():
|
---|
155 | """ test of maintaining two independant plotter instances
|
---|
156 | with different examples for init and call
|
---|
157 | """
|
---|
158 | x = np.linspace(0., 2*np.pi , 100)
|
---|
159 | plot1 = Plotter('plot1', x, 'r.:')
|
---|
160 | plot2 = Plotter('plot2')
|
---|
161 |
|
---|
162 | y1 = np.sin(x) * 7
|
---|
163 | plot1(y1)
|
---|
164 |
|
---|
165 | number_of_graphs_in_plot2 = 3
|
---|
166 | no = number_of_graphs_in_plot2 # short form
|
---|
167 |
|
---|
168 | # this is where you do your analysis...
|
---|
169 | y2 = np.empty( (no, len(x)) ) # prepare some space
|
---|
170 | y2_labels = [] # prepare labels
|
---|
171 | for k in range(no):
|
---|
172 | y2[k] = np.sin( (k+1)*x )
|
---|
173 | y2_labels.append('sin(%d*x)' % (k+1) )
|
---|
174 |
|
---|
175 | # plot the result of your analysis
|
---|
176 | plot2(y2, y2_labels)
|
---|
177 | raw_input('next') # do not forget this line, or your graph is lost
|
---|
178 |
|
---|
179 | plot1(np.cos(x) * 3.)
|
---|
180 | plot2.name += ' without labels!!!' # changing titles 'on the fly' is possible
|
---|
181 | plot2(y2)
|
---|
182 | raw_input('next') # DO NOT forget
|
---|
183 |
|
---|
184 |
|
---|
185 | def _test_CamPlotter():
|
---|
186 | """ test of CamPlotter """
|
---|
187 |
|
---|
188 | c1 = np.random.randn(1440)
|
---|
189 | c2 = np.linspace(0., 1., num=1440)
|
---|
190 | plot1 = CamPlotter('plot1')
|
---|
191 | plot2 = CamPlotter('plot2')
|
---|
192 |
|
---|
193 | plot1(c1)
|
---|
194 | plot2(c2)
|
---|
195 | raw_input('next')
|
---|
196 |
|
---|
197 | def _test_HistPlotter():
|
---|
198 | """ test of the HistPlotter """
|
---|
199 | plt.ion()
|
---|
200 |
|
---|
201 | data = np.random.randn(1000)
|
---|
202 | hp = HistPlotter('test hist plotter',34, (-5,4))
|
---|
203 |
|
---|
204 | hp(data, 'test-label')
|
---|
205 | raw_input('next')
|
---|
206 |
|
---|
207 | if __name__ == '__main__':
|
---|
208 | """ test the class """
|
---|
209 | _test_Plotter()
|
---|
210 | _test_CamPlotter()
|
---|
211 | _test_HistPlotter() |
---|