Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[hooke.git] / hooke / ui / gui / plot.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 class PlotObject(object):
20
21     def __init__(self):
22
23         '''
24         the plot destination
25         0=top
26         1=bottom
27         '''
28         self.destination=0
29
30         '''
31         self.vectors is a multidimensional array:
32         self.vectors[0]=plot1
33         self.vectors[1]=plot2
34         self.vectors[2]=plot3
35         etc.
36
37         2 curves in a x,y plot are:
38         [[[x1],[y1]],[[x2],[y2]]]
39         for example:
40             x1          y1              x2         y2
41         [[[1,2,3,4],[10,20,30,40]],[[3,6,9,12],[30,60,90,120]]]
42         x1 = self.vectors[0][0]
43         y1 = self.vectors[0][1]
44         x2 = self.vectors[1][0]
45         y2 = self.vectors[1][1]
46         '''
47         self.vectors=[]
48
49         '''
50         self.units is simpler. for each plot with N axes (x,y,z...) only N labels
51         can be made, regardless of the number of superimposed plots
52         so units for the double plot above is: [unitx, unity]
53
54         units are strings
55         '''
56         self.units=['', '']
57
58         '''
59         xaxes and yaxes directions. 0, 0 means the common +X=right, +Y=top directions
60         '''
61         self.xaxes = 0
62         self.yaxes = 0
63
64         self.filename = ''
65         self.title = '' #title
66
67         '''
68         styles: defines what is the style of the current plots. If undefined or None, it is line plot.
69         If an element of the list is 'scatter', the corresponding dataset
70         is drawn with scattered points and not a continuous line.
71         '''
72         self.styles = []
73
74         '''
75         colors: define what is the colour of the current plots
76         '''
77         self.colors = []
78
79     def add_set(self, x, y):
80         '''
81         Adds an x, y data set to the vectors.
82         '''
83         self.vectors.append([])
84         self.vectors[-1].append(x)
85         self.vectors[-1].append(y)
86
87     def remove_set(self, whichset):
88         '''
89         Removes a set
90         '''
91         #TODO: do we need 'waste' here?
92         waste = self.vectors.pop(whichset)
93
94     def normalize_vectors(self):
95         '''
96         Trims the vector lengths as to be equal in a plot.
97         '''
98         for index in range(0,len(self.vectors)):
99             vectors_to_plot=self.vectors[index]
100             lengths=[len(vector) for vector in vectors_to_plot]
101             if min(lengths) != max(lengths):
102                 for indexplot in range(0,len(vectors_to_plot)):
103                     self.vectors[index][indexplot] = self.vectors[index][indexplot][0:min(lengths)]
104