source: fact/tools/pyscripts/pyfact/plotters.py@ 13074

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