1 __author__="fccoelho@gmail.com"
2 __date__ ="$26/02/2009 10:44:29$"
3 __docformat__ = "restructuredtext en"
4 import Gnuplot
5 import numpy
6
7
9 '''
10 Real time plotting class based on Gnuplot
11 '''
13 self.gp = Gnuplot.Gnuplot(persist = persist, debug=debug)
14 self.plots = []
15
17 '''
18 Clears the figure.
19 '''
20 self.plots = []
21 self.gp.reset()
22
23 - def scatter(self,x,y,names=[],title='',style='points'):
24 """
25 Makes scatter plots from numpy arrays.
26 if arrays are multidimensional, multiple scatter plots will be generated, pairing rows.
27 """
28 if isinstance(x, numpy.ndarray):
29 if not isinstance(y, numpy.ndarray):
30 raise TypeError("If x is a numpy array, y must also be an array.")
31 if x.shape != y.shape:
32 raise ValueError("x, %s and y, %s arrays must have the same shape."%(x.shape,y.shape))
33 if names:
34 if len(names) != x.shape[0]:
35 raise ValueError("names list must have exactly %s items, but has %s."%(x.shape[0],len(names)))
36 else:
37 x = numpy.array(x)
38 y = numpy.array(y)
39
40
41 self.gp('set title "%s"'%title)
42 if not names:
43 names = ['s%s'%i for i in range(x.shape[0])]
44 if len(x.shape) > 1 and len(x.shape) <= 2:
45 i = 0
46 for n in range(x.shape[0]):
47 self.plots.append(Gnuplot.PlotItems.Data(x[n],y[n],title=names[i],with_=style))
48 i += 1
49 self.gp.plot(*tuple(self.plots))
50 elif len(x.shape) >2:
51 pass
52 else:
53
54 self.plots.append(Gnuplot.PlotItems.Data(x,y,title=names[0],with_=style))
55 self.gp.plot(*tuple(self.plots))
56
57 - def plotlines(self,data,names=[],title='',style='lines'):
58 '''
59 Create a sinlge/multiple line plot from a numpy array or record array.
60
61 :Parameters:
62 - `data`: must be a numpy array or record array, with series as rows
63 - `names`: is a list of strings to serve as legend labels
64 - `style`: plot styles from gnuplot: lines, boxes, points, linespoints, etc.
65 '''
66 self.gp('set title "%s"'%title)
67 if isinstance(data,numpy.core.records.recarray):
68 return self._linesFromRA(data,style)
69 if len(data.shape) > 1 and len(data.shape) <= 2:
70 i = 0
71 for row in data:
72 if names:
73 self.plots.append(Gnuplot.PlotItems.Data(row,title=names[i],with_=style))
74 else:
75 self.plots.append(Gnuplot.PlotItems.Data(row,with_=style))
76 i += 1
77 self.gp.plot(*tuple(self.plots))
78 elif len(data.shape) >2:
79 pass
80 else:
81
82 self.plots.append(Gnuplot.PlotItems.Data(data,title=names[0],with_=style))
83 self.gp.plot(*tuple(self.plots))
84
86 '''
87 Record-array specific code
88 '''
89 for n in data.dtype.names:
90 if len(data.shape) > 1 and len(data.shape) <= 2:
91 i = 0
92 for row in data[n]:
93 self.plots.append(Gnuplot.PlotItems.Data(row,title=n+':%s'%i,with_=style))
94 i += 1
95 elif len(data.shape) >2:
96 pass
97
98 else:
99 self.plots.append(Gnuplot.PlotItems.Data(data[n],title=n,with_=style))
100 self.gp.plot(*tuple(self.plots))
101
102 - def plothist(self,data, title='', names=[]):
103 '''
104 Create a sinlge/multiple Histogram plot from a numpy array or record array.
105
106 :Parameters:
107 - `data`: must be a numpy array or record array, with series as rows
108 - `names`: is a list of strings to serve as legend labels
109 '''
110 self.gp('set data style boxes')
111 self.gp('set title "%s"'%title)
112 if isinstance(data,numpy.core.records.recarray):
113 return self._histFromRA(data)
114 if not names:
115 names = ['s%s'%i for i in range(data.shape[0])]
116 if len(data.shape) > 1 and len(data.shape) <= 2:
117 for n,row in enumerate(data):
118 m,bins = numpy.histogram(row,normed=True,bins=50)
119 d = zip(bins[:-1],m)
120 self.plots.append(Gnuplot.PlotItems.Data(d,title=names[n]))
121 self.gp.plot(*tuple(self.plots))
122 elif len(data.shape) >2:
123 pass
124 else:
125 m,bins = numpy.histogram(data,normed=True,bins=50)
126 d = zip(bins[:-1],m)
127 self.plots.append(Gnuplot.PlotItems.Data(d,title=names[0]))
128 self.gp.plot(*tuple(self.plots))
129
131 '''
132 Record-array specific code
133 '''
134 for n in data.dtype.names:
135 if len(data.shape) > 1 and len(data.shape) <= 2:
136 i = 0
137 for row in data[n]:
138 m,bins = numpy.histogram(row,normed=True,bins=50)
139 d = zip(bins[:-1],m)
140 self.plots.append(Gnuplot.PlotItems.Data(d,title=n+':%s'%i))
141 i += 1
142 elif len(data.shape) > 2:
143 pass
144 else:
145 m,bins = numpy.histogram(data[n],normed=True,bins=50)
146 d = zip(bins[:-1],m)
147 self.plots.append(Gnuplot.PlotItems.Data(d,title=n))
148 self.gp.plot(*tuple(self.plots))
149
150 if __name__ == "__main__":
151 pass
152