##// END OF EJS Templates
Matplotlib version of BaseGraph
Marco Valdez -
r128:5a3562cdc96c
parent child
Show More
This diff has been collapsed as it changes many lines, (1211 lines changed) Show them Hide them
@@ -0,0 +1,1211
1 '''
2 Created on Feb 7, 2012
3
4 @autor $Author: dsuarez $
5 @version $Id: BaseGraph.py 117 2012-09-04 21:16:59Z dsuarez $
6
7 '''
8 #from __future__ import division
9 #import os
10 import numpy
11 #import sys
12 import time
13 import datetime
14
15 #import plplot
16 import matplotlib as mpl
17 mpl.use('TKAgg')
18 import matplotlib.pyplot as plt
19
20 import scitools.numpyutils as sn
21
22 def cmap1_init(colormap='gray'):
23
24 # if colormap == None:
25 # return
26 #
27 # ncolor = None
28 # rgb_lvl = None
29 #
30 # # Routine for defining a specific color map 1 in HLS space.
31 # # if gray is true, use basic grayscale variation from half-dark to light.
32 # # otherwise use false color variation from blue (240 deg) to red (360 deg).
33 #
34 # # Independent variable of control points.
35 # i = numpy.array((0., 1.))
36 # if colormap=='gray':
37 # ncolor = 256
38 # # Hue for control points. Doesn't matter since saturation is zero.
39 # h = numpy.array((0., 0.))
40 # # Lightness ranging from half-dark (for interest) to light.
41 # l = numpy.array((0.5, 1.))
42 # # Gray scale has zero saturation
43 # s = numpy.array((0., 0.))
44 #
45 # # number of cmap1 colours is 256 in this case.
46 # plplot.plscmap1n(ncolor)
47 # # Interpolate between control points to set up cmap1.
48 # plplot.plscmap1l(0, i, h, l, s)
49 #
50 # return None
51 #
52 # if colormap == 'jet':
53 # ncolor = 256
54 # pos = numpy.zeros((ncolor))
55 # r = numpy.zeros((ncolor))
56 # g = numpy.zeros((ncolor))
57 # b = numpy.zeros((ncolor))
58 #
59 # for i in range(ncolor):
60 # if(i <= 35.0/100*(ncolor-1)): rf = 0.0
61 # elif (i <= 66.0/100*(ncolor-1)): rf = (100.0/31)*i/(ncolor-1) - 35.0/31
62 # elif (i <= 89.0/100*(ncolor-1)): rf = 1.0
63 # else: rf = (-100.0/22)*i/(ncolor-1) + 111.0/22
64 #
65 # if(i <= 12.0/100*(ncolor-1)): gf = 0.0
66 # elif(i <= 38.0/100*(ncolor-1)): gf = (100.0/26)*i/(ncolor-1) - 12.0/26
67 # elif(i <= 64.0/100*(ncolor-1)): gf = 1.0
68 # elif(i <= 91.0/100*(ncolor-1)): gf = (-100.0/27)*i/(ncolor-1) + 91.0/27
69 # else: gf = 0.0
70 #
71 # if(i <= 11.0/100*(ncolor-1)): bf = (50.0/11)*i/(ncolor-1) + 0.5
72 # elif(i <= 34.0/100*(ncolor-1)): bf = 1.0
73 # elif(i <= 65.0/100*(ncolor-1)): bf = (-100.0/31)*i/(ncolor-1) + 65.0/31
74 # else: bf = 0
75 #
76 # r[i] = rf
77 # g[i] = gf
78 # b[i] = bf
79 #
80 # pos[i] = float(i)/float(ncolor-1)
81 #
82 #
83 # plplot.plscmap1n(ncolor)
84 # plplot.plscmap1l(1, pos, r, g, b)
85 #
86 #
87 #
88 # if colormap=='br_green':
89 # ncolor = 256
90 # # Hue ranges from blue (240 deg) to red (0 or 360 deg)
91 # h = numpy.array((240., 0.))
92 # # Lightness and saturation are constant (values taken from C example).
93 # l = numpy.array((0.6, 0.6))
94 # s = numpy.array((0.8, 0.8))
95 #
96 # # number of cmap1 colours is 256 in this case.
97 # plplot.plscmap1n(ncolor)
98 # # Interpolate between control points to set up cmap1.
99 # plplot.plscmap1l(0, i, h, l, s)
100 #
101 # return None
102 #
103 # if colormap=='tricolor':
104 # ncolor = 3
105 # # Hue ranges from blue (240 deg) to red (0 or 360 deg)
106 # h = numpy.array((240., 0.))
107 # # Lightness and saturation are constant (values taken from C example).
108 # l = numpy.array((0.6, 0.6))
109 # s = numpy.array((0.8, 0.8))
110 #
111 # # number of cmap1 colours is 256 in this case.
112 # plplot.plscmap1n(ncolor)
113 # # Interpolate between control points to set up cmap1.
114 # plplot.plscmap1l(0, i, h, l, s)
115 #
116 # return None
117 #
118 # if colormap == 'rgb' or colormap == 'rgb666':
119 #
120 # color_sz = 6
121 # ncolor = color_sz*color_sz*color_sz
122 # pos = numpy.zeros((ncolor))
123 # r = numpy.zeros((ncolor))
124 # g = numpy.zeros((ncolor))
125 # b = numpy.zeros((ncolor))
126 # ind = 0
127 # for ri in range(color_sz):
128 # for gi in range(color_sz):
129 # for bi in range(color_sz):
130 # r[ind] = ri/(color_sz-1.0)
131 # g[ind] = gi/(color_sz-1.0)
132 # b[ind] = bi/(color_sz-1.0)
133 # pos[ind] = ind/(ncolor-1.0)
134 # ind += 1
135 # rgb_lvl = [6,6,6] #Levels for RGB colors
136 #
137 # if colormap == 'rgb676':
138 # ncolor = 6*7*6
139 # pos = numpy.zeros((ncolor))
140 # r = numpy.zeros((ncolor))
141 # g = numpy.zeros((ncolor))
142 # b = numpy.zeros((ncolor))
143 # ind = 0
144 # for ri in range(8):
145 # for gi in range(8):
146 # for bi in range(4):
147 # r[ind] = ri/(6-1.0)
148 # g[ind] = gi/(7-1.0)
149 # b[ind] = bi/(6-1.0)
150 # pos[ind] = ind/(ncolor-1.0)
151 # ind += 1
152 # rgb_lvl = [6,7,6] #Levels for RGB colors
153 #
154 # if colormap == 'rgb685':
155 # ncolor = 6*8*5
156 # pos = numpy.zeros((ncolor))
157 # r = numpy.zeros((ncolor))
158 # g = numpy.zeros((ncolor))
159 # b = numpy.zeros((ncolor))
160 # ind = 0
161 # for ri in range(8):
162 # for gi in range(8):
163 # for bi in range(4):
164 # r[ind] = ri/(6-1.0)
165 # g[ind] = gi/(8-1.0)
166 # b[ind] = bi/(5-1.0)
167 # pos[ind] = ind/(ncolor-1.0)
168 # ind += 1
169 # rgb_lvl = [6,8,5] #Levels for RGB colors
170 #
171 # if colormap == 'rgb884':
172 # ncolor = 8*8*4
173 # pos = numpy.zeros((ncolor))
174 # r = numpy.zeros((ncolor))
175 # g = numpy.zeros((ncolor))
176 # b = numpy.zeros((ncolor))
177 # ind = 0
178 # for ri in range(8):
179 # for gi in range(8):
180 # for bi in range(4):
181 # r[ind] = ri/(8-1.0)
182 # g[ind] = gi/(8-1.0)
183 # b[ind] = bi/(4-1.0)
184 # pos[ind] = ind/(ncolor-1.0)
185 # ind += 1
186 # rgb_lvl = [8,8,4] #Levels for RGB colors
187 #
188 # if ncolor == None:
189 # raise ValueError, 'The colormap selected is not valid'
190 #
191 # plplot.plscmap1n(ncolor)
192 # plplot.plscmap1l(1, pos, r, g, b)
193 #
194 # return rgb_lvl
195 pass
196
197 def setColormap(colormap='jet'):
198 # cmap1_init(colormap)
199 pass
200
201 def savePlplot(filename,width,height):
202 # curr_strm = plplot.plgstrm()
203 # save_strm = plplot.plmkstrm()
204 # plplot.plsetopt('geometry', '%dx%d'%(width,height))
205 # plplot.plsdev('png')
206 # plplot.plsfnam(filename)
207 # plplot.plcpstrm(curr_strm,0)
208 # plplot.plreplot()
209 # plplot.plend1()
210 # plplot.plsstrm(curr_strm)
211 pass
212
213 def initMatplotlib(indexFig,ncol,nrow,winTitle,width,height):
214
215 plt.ioff()
216 fig = plt.figure(indexFig)
217 fig.canvas.manager.set_window_title(winTitle)
218 fig.canvas.manager.resize(width,height)
219 # fig.add_subplot(nrow,ncol,1)
220 plt.ion()
221
222 def setNewPage():
223 # plplot.plbop()
224 # plplot.pladv(0)
225 plt.clf()
226
227 def closePage():
228 # plplot.pleop()
229 pass
230
231 def clearData(objGraph):
232 objGraph.plotBox(objGraph.xrange[0], objGraph.xrange[1], objGraph.yrange[0], objGraph.yrange[1], 'bc', 'bc')
233
234 objGraph.setColor(15) #Setting Line Color to White
235
236 if objGraph.datatype == 'complex':
237 objGraph.basicXYPlot(objGraph.xdata,objGraph.ydata.real)
238 objGraph.basicXYPlot(objGraph.xdata,objGraph.ydata.imag)
239
240 if objGraph.datatype == 'real':
241 objGraph.basicXYPlot(objGraph.xdata,objGraph.ydata)
242
243 objGraph.setColor(1) #Setting Line Color to Black
244 # objGraph.setLineStyle(2)
245 # objGraph.plotBox(objGraph.xrange[0], objGraph.xrange[1], objGraph.yrange[0], objGraph.yrange[1], 'bcntg', 'bc')
246 # objGraph.setLineStyle(1)
247
248 def setFigure(indexFig):
249 # plplot.plsstrm(indexFig)
250 plt.figure(indexFig)
251
252 def refresh():
253 # plplot.refresh()
254 plt.draw()
255
256 def show():
257 # plplot.plspause(True)
258 # plplot.plend()
259 plt.ioff()
260 plt.show()
261 plt.ion()
262
263 def setPlTitle(pltitle,color, szchar=0.7):
264 # setSubpages(1, 0)
265 # plplot.pladv(0)
266 # plplot.plvpor(0., 1., 0., 1.)
267 #
268 # if color == 'black':
269 # plplot.plcol0(1)
270 # if color == 'white':
271 # plplot.plcol0(15)
272 #
273 # plplot.plschr(0.0,szchar)
274 # plplot.plmtex('t',-1., 0.5, 0.5, pltitle)
275 pass
276
277 def setSubpages(ncol,nrow):
278 # plplot.plssub(ncol,nrow)
279 pass
280
281 class BaseGraph:
282
283 __name = None
284 __xpos = None
285 __ypos = None
286 __subplot = None
287 __xg = None
288 __yg = None
289 __axesId = None
290 xdata = None
291 ydata = None
292 getGrid = True
293 xaxisIsTime = False
294 deltax = None
295 xmin = None
296 xmax = None
297
298 def __init__(self,name,subplot,xpos,ypos,xlabel,ylabel,title,szchar,xrange,yrange,zrange=None,deltax=1.0):
299
300 self.setName(name)
301 self.setScreenPos(xpos, ypos)
302 self.setSubPlot(subplot)
303 self.setXYZrange(xrange,yrange,zrange)
304 self.setSizeOfChar(szchar)
305 self.setLabels(xlabel,ylabel,title)
306 self.getGrid = True
307 self.xaxisIsTime = False
308 self.deltax = deltax
309 pass
310
311 def makeAxes(self,indexFig,nrow,ncol,subplot):
312 fig = plt.figure(indexFig)
313 self.__axesId = fig.add_subplot(nrow,ncol,subplot)
314
315 def setParentAxesId(self,parent):
316 self.__axesId = parent.__axesId
317
318 def setName(self, name):
319 self.__name = name
320
321 def setScreenPos(self,xpos,ypos):
322 self.__xpos = xpos
323 self.__ypos = ypos
324
325 def setSubPlot(self,subplot):
326 self.__subplot = subplot
327
328 def setXYZrange(self,xrange,yrange,zrange):
329 self.xrange = xrange
330 self.yrange = yrange
331 self.zrange = zrange
332
333 def setSizeOfChar(self,szchar):
334 self.__szchar = szchar
335
336 def setLabels(self,xlabel=None,ylabel=None,title=None):
337 if xlabel != None: self.xlabel = xlabel
338 if ylabel != None: self.ylabel = ylabel
339 if title != None: self.title = title
340
341 def setXYData(self,xdata=None,ydata=None,datatype='real'):
342 if ((xdata != None) and (ydata != None)):
343 self.xdata = xdata
344 self.ydata = ydata
345 self.datatype = datatype
346 if ((self.xdata == None) and (self.ydata == None)):
347 return None
348 return 1
349
350 def setLineStyle(self, style):
351 # plplot.pllsty(style)
352 pass
353
354 def setColor(self, color):
355 # plplot.plcol0(color)
356 pass
357
358 def setXAxisAsTime(self, value=False):
359 self.xaxisIsTime = value
360
361 def basicLineTimePlot(self, x, y, colline=1):
362 # plplot.plcol0(colline)
363 # plplot.plline(x, y)
364 # plplot.plcol0(1)
365 ax = self.__axesId
366 if self.setXYData() == None:
367 ax.plot(x,y)
368 else:
369 ax.lines[0].set_data(x,y)
370
371 def basicXYPlot(self, x, y):
372 # plplot.plline(x, y)
373 ax = self.__axesId
374 if self.setXYData() == None:
375 ax.plot(x,y)
376 else:
377 ax.lines[0].set_data(x,y)
378
379 def basicPcolorPlot(self, data, x, y, xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
380 #
381 # if xmin == None: xmin = x[0]
382 # if xmax == None: xmax = x[-1]
383 # if ymin == None: ymin = y[0]
384 # if ymax == None: ymax = y[-1]
385 # if zmin == None: zmin = numpy.nanmin(data)
386 # if zmax == None: zmax = numpy.nanmax(data)
387 #
388 # plplot.plimage(data,
389 # float(x[0]),
390 # float(x[-1]),
391 # float(y[0]),
392 # float(y[-1]),
393 # float(zmin),
394 # float(zmax),
395 # float(xmin),
396 # float(xmax),
397 # float(ymin),
398 # float(ymax)
399 # )
400 pass
401
402 def __getBoxpltr(self, x, y, deltax=None, deltay=None):
403
404 # if not(len(x)>0 and len(y)>0):
405 # raise ValueError, 'x axis and y axis are empty'
406 #
407 # if deltax == None: deltax = x[-1] - x[-2]
408 # if deltay == None: deltay = y[-1] - y[-2]
409 #
410 # x1 = numpy.append(x, x[-1] + deltax)
411 # y1 = numpy.append(y, y[-1] + deltay)
412 #
413 # xg = (numpy.multiply.outer(x1, numpy.ones(len(y1))))
414 # yg = (numpy.multiply.outer(numpy.ones(len(x1)), y1))
415 #
416 # self.__xg = xg
417 # self.__yg = yg
418 #
419 # return xg, yg
420 pass
421
422 def advPcolorPlot(self, data, x, y, xmin=None, xmax=None, ymin=None, ymax=None, zmin=0., zmax=0., deltax=1.0, deltay=None, getGrid = True):
423 # if getGrid:
424 # xg, yg = self.__getBoxpltr(x, y, deltax, deltay)
425 # else:
426 # xg = self.__xg
427 # yg = self.__yg
428 #
429 # plplot.plimagefr(data,
430 # float(xmin),
431 # float(xmax),
432 # float(ymin),
433 # float(ymax),
434 # 0.,
435 # 0.,
436 # float(zmin),
437 # float(zmax),
438 # plplot.pltr2,
439 # xg,
440 # yg)
441
442 ax = self.__axesId
443 # if self.setXYData() == None:
444 ax.pcolormesh(x,y,data.T,vmin=zmin,vmax=zmax)
445 # else:
446 # ax.lines[0].set_data(x,y)
447
448
449 def colorbarPlot(self, xmin=0., xmax=1., ymin=0., ymax=1.):
450 # data = numpy.arange(256)
451 # data = numpy.reshape(data, (1,-1))
452 # plplot.plimage(data,
453 # float(xmin),
454 # float(xmax),
455 # float(ymin),
456 # float(ymax),
457 # 0.,
458 # 255.,
459 # float(xmin),
460 # float(xmax),
461 # float(ymin),
462 # float(ymax))
463 ax = self.__axesId
464 cax, kw = mpl.colorbar.make_axes(ax)
465 norm = mpl.colors.Normalize(vmin=ymin, vmax=ymax)
466 cb = mpl.colorbar.ColorbarBase(cax,norm=norm,**kw)
467 self.__colorbarId = cb
468 cb.set_label(self.title)
469
470 def plotBox(self, xmin, xmax, ymin, ymax, xopt, yopt, nolabels=False):
471
472 # plplot.plschr(0.0,self.__szchar-0.05)
473 # plplot.pladv(self.__subplot)
474 # plplot.plvpor(self.__xpos[0], self.__xpos[1], self.__ypos[0], self.__ypos[1])
475 # plplot.plwind(float(xmin), # self.xrange[0]
476 # float(xmax), # self.xrange[1]
477 # float(ymin), # self.yrange[0]
478 # float(ymax) # self.yrange[1]
479 # )
480 #
481 #
482 #
483 # if self.xaxisIsTime:
484 # plplot.pltimefmt('%H:%M')
485 # timedelta = (xmax - xmin + 1)/8.
486 # plplot.plbox(xopt, timedelta, 3, yopt, 0.0, 0)
487 # else:
488 # plplot.plbox(xopt, 0.0, 0, yopt, 0.0, 0)
489 #
490 #
491 # if not(nolabels):
492 # plplot.pllab(self.xlabel, self.ylabel, self.title)
493
494 ax = self.__axesId
495 ax.set_xlim([xmin,xmax])
496 ax.set_ylim([ymin,ymax])
497
498 if not(nolabels):
499 ax.set_xlabel(self.xlabel)
500 ax.set_ylabel(self.ylabel)
501 ax.set_title(self.title)
502
503 # fig = plt.gcf()
504 # nrows = self.
505 # fig.add_subplot
506 # print
507 pass
508
509 def delLabels(self):
510 # self.setColor(15) #Setting Line Color to White
511 # plplot.pllab(self.xlabel, self.ylabel, self.title)
512 # self.setColor(1) #Setting Line Color to Black
513 pass
514
515 def plotImage(self,x,y,z,xrange,yrange,zrange):
516 # xi = x[0]
517 # xf = x[-1]
518 # yi = y[0]
519 # yf = y[-1]
520 #
521 # plplot.plimage(z,
522 # float(xi),
523 # float(xf),
524 # float(yi),
525 # float(yf),
526 # float(zrange[0]),
527 # float(zrange[1]),
528 # float(xi),
529 # float(xf),
530 # float(yrange[0]),
531 # yrange[1])
532 pass
533
534 class LinearPlot:
535
536 linearObjDic = {}
537 __xpos = None
538 __ypos = None
539
540 def __init__(self,indexFig,nsubplot,winTitle):
541 self.width = 700
542 self.height = 150
543 self.indexFig = indexFig
544 self.ncol = 1
545 self.nrow = nsubplot
546 initMatplotlib(indexFig,self.ncol,self.nrow,winTitle,self.width,self.height)
547
548 def setFigure(self,indexFig):
549 setFigure(indexFig)
550
551 def setPosition(self):
552
553 xi = 0.07; xf = 0.9 #0.8,0.7,0.5
554 yi = 0.15; yf = 0.8
555
556 xpos = [xi,xf]
557 ypos = [yi,yf]
558
559 self.__xpos = xpos
560 self.__ypos = ypos
561
562 return xpos,ypos
563
564 def show(self):
565 show()
566
567 def refresh(self):
568 refresh()
569
570 def setup(self,subplot,xmin,xmax,ymin,ymax,title,xlabel,ylabel):
571 szchar = 1.10
572 name = 'linear'
573 key = name + '%d'%subplot
574 xrange = [xmin,xmax]
575 yrange = [ymin,ymax]
576
577 xpos,ypos = self.setPosition()
578 linearObj = BaseGraph(name,subplot,xpos,ypos,xlabel,ylabel,title,szchar,xrange,yrange)
579 linearObj.makeAxes(self.indexFig,self.nrow,self.ncol,subplot)
580 linearObj.plotBox(linearObj.xrange[0], linearObj.xrange[1], linearObj.yrange[0], linearObj.yrange[1], 'bcnst', 'bcnstv')
581 self.linearObjDic[key] = linearObj
582
583
584 def plot(self,subplot,x,y,type='abs'):
585 name = 'linear'
586 key = name + '%d'%subplot
587
588 linearObj = self.linearObjDic[key]
589 #linearObj.plotBox(linearObj.xrange[0], linearObj.xrange[1], linearObj.yrange[0], linearObj.yrange[1], 'bcst', 'bcst')
590
591 # if linearObj.setXYData() != None:
592 # clearData(linearObj)
593 # else:
594 # if type.lower() == 'simple':
595 # linearObj.setXYData(x,y,'real')
596 # if type.lower() == 'power':
597 # linearObj.setXYData(x,abs(y),'real')
598 # if type.lower() == 'iq':
599 # linearObj.setXYData(x,y,'complex')
600
601 if type.lower() == 'simple':
602 colline = 9
603 linearObj.basicLineTimePlot(x, y)
604 linearObj.setXYData(x,y,'real')
605
606 if type.lower() == 'abs':
607 colline = 9
608 linearObj.basicLineTimePlot(x, abs(y), colline)
609 linearObj.setXYData(x,abs(y),'real')
610
611 if type.lower() == 'iq':
612 colline = 9
613 linearObj.basicLineTimePlot(x=x, y=y.real, colline=colline)
614 colline = 13
615 linearObj.basicLineTimePlot(x=x, y=y.imag, colline=colline)
616 linearObj.setXYData(x,y,'complex')
617
618 # linearObj.plotBox(linearObj.xrange[0], linearObj.xrange[1], linearObj.yrange[0], linearObj.yrange[1], 'bcst', 'bcst')
619
620 pass
621
622
623 class PcolorPlot:
624
625 pcolorObjDic = {}
626 colorbarObjDic = {}
627 pwprofileObjDic = {}
628 showColorbar = None
629 showPowerProfile = None
630 XAxisAsTime = None
631 width = None
632 height = None
633 __spcxpos = None
634 __spcypos = None
635 __cmapxpos = None
636 __cmapypos = None
637 __profxpos = None
638 __profypos = None
639 __lastTitle = None
640
641 def __init__(self,indexFig,nsubplot,winTitle,colormap,showColorbar,showPowerProfile,XAxisAsTime):
642
643 self.width = 460
644 self.height = 300
645 self.showColorbar = showColorbar
646 self.showPowerProfile = showPowerProfile
647 self.XAxisAsTime = XAxisAsTime
648
649 ncol = int(numpy.sqrt(nsubplot)+0.9)
650 nrow = int(nsubplot*1./ncol + 0.9)
651
652 self.ncol = ncol
653 self.nrow = nrow
654 self.indexFig = indexFig
655
656 initMatplotlib(indexFig,ncol,nrow,winTitle,self.width,self.height)
657 setColormap(colormap)
658
659 def setFigure(self,indexFig):
660 setFigure(indexFig)
661
662 def setSpectraPos(self): #modificar valores de acuerdo al colorbar y pwprofile
663 if self.showPowerProfile: xi = 0.09; xf = 0.6 #0.075
664 else: xi = 0.15; xf = 0.8 #0.8,0.7,0.5
665 yi = 0.15; yf = 0.80
666
667 xpos = [xi,xf]
668 ypos = [yi,yf]
669
670 self.__spcxpos = xpos
671 self.__spcypos = ypos
672
673 return xpos,ypos
674
675 def setColorbarScreenPos(self):
676
677 xi = self.__spcxpos[1] + 0.03; xf = xi + 0.03
678 yi = self.__spcypos[0]; yf = self.__spcypos[1]
679
680 xpos = [xi,xf]
681 ypos = [yi,yf]
682
683 self.__cmapxpos = xpos
684 self.__cmapypos = ypos
685
686 return xpos,ypos
687
688 def setPowerprofileScreenPos(self):
689
690 xi = self.__cmapxpos[1] + 0.07; xf = xi + 0.25
691 yi = self.__spcypos[0]; yf = self.__spcypos[1]
692
693 xpos = [xi,xf]
694 ypos = [yi,yf]
695
696 self.__profxpos = [xi,xf]
697 self.__profypos = [yi,yf]
698
699 return xpos,ypos
700
701 def createObjects(self,subplot,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel):
702
703 '''
704 Crea los objetos necesarios para un subplot
705 '''
706
707 # Config Spectra plot
708
709 szchar = 0.7
710 name = 'spc'
711 key = name + '%d'%subplot
712 xrange = [xmin,xmax]
713 yrange = [ymin,ymax]
714 zrange = [zmin,zmax]
715
716 xpos,ypos = self.setSpectraPos()
717 pcolorObj = BaseGraph(name,subplot,xpos,ypos,xlabel,ylabel,title,szchar,xrange,yrange,zrange)
718 #pcolorObj.makeAxes(self.indexFig,self.nrow,self.ncol,subplot)
719 self.pcolorObjDic[key] = pcolorObj
720
721 # Config Colorbar
722 if self.showColorbar:
723 szchar = 0.65
724 name = 'colorbar'
725 key = name + '%d'%subplot
726
727 xpos,ypos = self.setColorbarScreenPos()
728 xrange = [0.,1.]
729 yrange = [zmin,zmax]
730 cmapObj = BaseGraph(name,subplot,xpos,ypos,'','','dB',szchar,xrange,yrange)
731 self.colorbarObjDic[key] = cmapObj
732
733 # Config Power profile
734 if self.showPowerProfile:
735 szchar = 0.55
736 name = 'pwprofile'
737 key = name + '%d'%subplot
738
739 xpos,ypos = self.setPowerprofileScreenPos()
740 xrange = [zmin,zmax]
741 yrange = [ymin,ymax]
742 powObj = BaseGraph(name,subplot,xpos,ypos,'dB','','Power Profile',szchar,xrange,yrange)
743 #powObj.makeAxes(self.indexFig,self.nrow,self.ncol,subplot)
744 self.pwprofileObjDic[key] = powObj
745
746 def setNewPage(self, pltitle='No title'):
747 szchar = 0.7
748 setNewPage()
749 setPlTitle(pltitle,'black', szchar=szchar)
750 setSubpages(self.ncol, self.nrow)
751
752 def closePage(self):
753 closePage()
754
755 def show(self):
756 show()
757
758 def iniPlot(self,subplot):
759 '''
760 Inicializa los subplots con su frame, titulo, etc
761 '''
762
763 # Config Spectra plot
764 name = 'spc'
765 key = name + '%d'%subplot
766
767 pcolorObj = self.pcolorObjDic[key]
768 pcolorObj.makeAxes(self.indexFig,self.nrow,self.ncol,subplot)
769 pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], 'bcnst', 'bcnstv')
770
771 # Config Colorbar
772 if self.showColorbar:
773 name = 'colorbar'
774 key = name + '%d'%subplot
775
776 cmapObj = self.colorbarObjDic[key]
777 cmapObj.setParentAxesId(pcolorObj)
778 # cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], 'bc', 'bcmtsv')
779 cmapObj.colorbarPlot(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1])
780 # cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], 'bc', 'bcmtsv')
781
782 # Config Power profile
783 if self.showPowerProfile:
784 name = 'pwprofile'
785 key = name + '%d'%subplot
786
787 powObj = self.pwprofileObjDic[key]
788 powObj.setLineStyle(2)
789 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bcntg', 'bc')
790 powObj.setLineStyle(1)
791 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bc', 'bc')
792
793 def printTitle(self,pltitle):
794 # if self.__lastTitle != None:
795 # setPlTitle(self.__lastTitle,'white')
796 #
797 # self.__lastTitle = pltitle
798
799 setPlTitle(pltitle,'black')
800
801 # setSubpages(self.ncol,self.nrow)
802
803 def plot(self,subplot,x,y,z,subtitle=''):
804 # Spectra plot
805
806 name = 'spc'
807 key = name + '%d'%subplot
808
809 # newx = [x[0],x[-1]]
810 pcolorObj = self.pcolorObjDic[key]
811 # pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], 'bcst', 'bcst')
812
813 #pcolorObj.delLabels()
814 pcolorObj.setLabels(title=subtitle)
815
816 deltax = None; deltay = None
817
818 pcolorObj.advPcolorPlot(z,
819 x,
820 y,
821 xmin=pcolorObj.xrange[0],
822 xmax=pcolorObj.xrange[1],
823 ymin=pcolorObj.yrange[0],
824 ymax=pcolorObj.yrange[1],
825 zmin=pcolorObj.zrange[0],
826 zmax=pcolorObj.zrange[1],
827 deltax=deltax,
828 deltay=deltay,
829 getGrid=pcolorObj.getGrid)
830
831 #Solo se calcula la primera vez que se ingresa a la funcion
832 pcolorObj.getGrid = False
833
834 #pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], 'bcst', 'bcst', nolabels=True)
835
836 # Power Profile
837 if self.showPowerProfile:
838 power = numpy.average(z, axis=0)
839 name = 'pwprofile'
840 key = name + '%d'%subplot
841 powObj = self.pwprofileObjDic[key]
842
843 if powObj.setXYData() != None:
844 #clearData(powObj)
845 powObj.setLineStyle(2)
846 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bcntg', 'bc')
847 powObj.setLineStyle(1)
848 else:
849 powObj.setXYData(power,y)
850
851 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bc', 'bc')
852 powObj.basicXYPlot(power,y)
853 powObj.setXYData(power,y)
854
855 def savePlot(self,indexFig,filename):
856
857 width = self.width*self.ncol
858 hei = self.height*self.nrow
859 savePlplot(filename,width,hei)
860
861 def refresh(self):
862 refresh()
863
864
865 class RtiPlot:
866
867 pcolorObjDic = {}
868 colorbarObjDic = {}
869 pwprofileObjDic = {}
870 showColorbar = None
871 showPowerProfile = None
872 XAxisAsTime = None
873 widht = None
874 height = None
875 __rtixpos = None
876 __rtiypos = None
877 __cmapxpos = None
878 __cmapypos = None
879 __profxpos = None
880 __profypos = None
881
882 def __init__(self,indexFig,nsubplot,winTitle,colormap,showColorbar,showPowerProfile,XAxisAsTime):
883 self.width = 700
884 self.height = 150
885 self.showColorbar = showColorbar
886 self.showPowerProfile = showPowerProfile
887 self.XAxisAsTime = XAxisAsTime
888
889 ncol = 1
890 nrow = nsubplot
891 initMatplotlib(indexFig,ncol,nrow,winTitle,self.width,self.height)
892 setColormap(colormap)
893 self.ncol = ncol
894 self.nrow = nrow
895
896 def setFigure(self,indexFig):
897 setFigure(indexFig)
898
899 def setRtiScreenPos(self):
900
901 if self.showPowerProfile: xi = 0.07; xf = 0.65
902 else: xi = 0.07; xf = 0.9
903 yi = 0.15; yf = 0.80
904
905 xpos = [xi,xf]
906 ypos = [yi,yf]
907
908 self.__rtixpos = xpos
909 self.__rtiypos = ypos
910
911 return xpos,ypos
912
913 def setColorbarScreenPos(self):
914
915 xi = self.__rtixpos[1] + 0.03; xf = xi + 0.03
916
917 yi = self.__rtiypos[0]; yf = self.__rtiypos[1]
918
919 xpos = [xi,xf]
920 ypos = [yi,yf]
921
922 self.__cmapxpos = xpos
923 self.__cmapypos = ypos
924
925 return xpos,ypos
926
927 def setPowerprofileScreenPos(self):
928
929 xi = self.__cmapxpos[1] + 0.05; xf = xi + 0.20
930
931 yi = self.__rtiypos[0]; yf = self.__rtiypos[1]
932
933 xpos = [xi,xf]
934 ypos = [yi,yf]
935
936 self.__profxpos = [xi,xf]
937 self.__profypos = [yi,yf]
938
939 return xpos,ypos
940
941 def setup(self,subplot,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel,timedata,timezone='lt',npoints=100):
942 # Config Rti plot
943 szchar = 1.10
944 name = 'rti'
945 key = name + '%d'%subplot
946
947 # xmin, xmax --> minHour, max Hour : valores que definen el ejex x=[horaInicio,horaFinal]
948 thisDateTime = datetime.datetime.fromtimestamp(timedata)
949 startDateTime = datetime.datetime(thisDateTime.year,thisDateTime.month,thisDateTime.day,xmin,0,0)
950 endDateTime = datetime.datetime(thisDateTime.year,thisDateTime.month,thisDateTime.day,xmax,59,59)
951 deltaTime = 0
952 if timezone == 'lt':
953 deltaTime = time.timezone
954 startTimeInSecs = time.mktime(startDateTime.timetuple()) - deltaTime
955 endTimeInSecs = time.mktime(endDateTime.timetuple()) - deltaTime
956
957 xrange = [startTimeInSecs,endTimeInSecs]
958 totalTimeInXrange = endTimeInSecs - startTimeInSecs + 1.
959 deltax = totalTimeInXrange / npoints
960
961 yrange = [ymin,ymax]
962 zrange = [zmin,zmax]
963
964 xpos,ypos = self.setRtiScreenPos()
965 pcolorObj = BaseGraph(name,subplot,xpos,ypos,xlabel,ylabel,title,szchar,xrange,yrange,zrange,deltax)
966 if self.XAxisAsTime:
967 pcolorObj.setXAxisAsTime(self.XAxisAsTime)
968 xopt = 'bcnstd'
969 yopt = 'bcnstv'
970 else:
971 xopt = 'bcnst'
972 yopt = 'bcnstv'
973
974 pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], xopt, yopt)
975 self.pcolorObjDic[key] = pcolorObj
976
977
978 # Config Colorbar
979 if self.showColorbar:
980 szchar = 0.9
981 name = 'colorbar'
982 key = name + '%d'%subplot
983
984 xpos,ypos = self.setColorbarScreenPos()
985 xrange = [0.,1.]
986 yrange = [zmin,zmax]
987 cmapObj = BaseGraph(name,subplot,xpos,ypos,'','','dB',szchar,xrange,yrange)
988 cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], 'bc', 'bcm')
989 cmapObj.colorbarPlot(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1])
990 cmapObj.plotBox(cmapObj.xrange[0], cmapObj.xrange[1], cmapObj.yrange[0], cmapObj.yrange[1], 'bc', 'bcmtsv')
991 self.colorbarObjDic[key] = cmapObj
992
993
994 # Config Power profile
995 if self.showPowerProfile:
996 szchar = 0.8
997 name = 'pwprofile'
998 key = name + '%d'%subplot
999
1000 xpos,ypos = self.setPowerprofileScreenPos()
1001 xrange = [zmin,zmax]
1002 yrange = [ymin,ymax]
1003 powObj = BaseGraph(name,subplot,xpos,ypos,'dB','','Power Profile',szchar,xrange,yrange)
1004 powObj.setLineStyle(2)
1005 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bcntg', 'bc')
1006 powObj.setLineStyle(1)
1007 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bc', 'bc')
1008 self.pwprofileObjDic[key] = powObj
1009
1010
1011 def plot(self,subplot,x,y,z):
1012 # RTI plot
1013 name = 'rti'
1014 key = name + '%d'%subplot
1015
1016 data = numpy.reshape(z, (1,-1))
1017 data = numpy.abs(data)
1018 data = 10*numpy.log10(data)
1019 newx = [x,x+1]
1020
1021 pcolorObj = self.pcolorObjDic[key]
1022
1023 if pcolorObj.xaxisIsTime:
1024 xopt = 'bcstd'
1025 yopt = 'bcst'
1026 else:
1027 xopt = 'bcst'
1028 yopt = 'bcst'
1029
1030 pcolorObj.plotBox(pcolorObj.xrange[0], pcolorObj.xrange[1], pcolorObj.yrange[0], pcolorObj.yrange[1], xopt, yopt)
1031
1032 deltax = pcolorObj.deltax
1033 deltay = None
1034
1035 if pcolorObj.xmin == None and pcolorObj.xmax == None:
1036 pcolorObj.xmin = x
1037 pcolorObj.xmax = x
1038
1039 if x >= pcolorObj.xmax:
1040 xmin = x
1041 xmax = x + deltax
1042 x = [x]
1043 pcolorObj.advPcolorPlot(data,
1044 x,
1045 y,
1046 xmin=xmin,
1047 xmax=xmax,
1048 ymin=pcolorObj.yrange[0],
1049 ymax=pcolorObj.yrange[1],
1050 zmin=pcolorObj.zrange[0],
1051 zmax=pcolorObj.zrange[1],
1052 deltax=deltax,
1053 deltay=deltay,
1054 getGrid=pcolorObj.getGrid)
1055
1056 pcolorObj.xmin = xmin
1057 pcolorObj.xmax = xmax
1058
1059
1060 # Power Profile
1061 if self.showPowerProfile:
1062 data = numpy.reshape(data,(numpy.size(data)))
1063 name = 'pwprofile'
1064 key = name + '%d'%subplot
1065 powObj = self.pwprofileObjDic[key]
1066
1067 if powObj.setXYData() != None:
1068 clearData(powObj)
1069 powObj.setLineStyle(2)
1070 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bcntg', 'bc')
1071 powObj.setLineStyle(1)
1072 else:
1073 powObj.setXYData(data,y)
1074
1075 powObj.plotBox(powObj.xrange[0], powObj.xrange[1], powObj.yrange[0], powObj.yrange[1], 'bc', 'bc')
1076 powObj.basicXYPlot(data,y)
1077 powObj.setXYData(data,y)
1078
1079 def savePlot(self,indexFig,filename):
1080
1081 width = self.width*self.ncol
1082 hei = self.height*self.nrow
1083 savePlplot(filename,width,hei)
1084
1085 def refresh(self):
1086 refresh()
1087
1088
1089 if __name__ == '__main__':
1090
1091 """
1092 Ejemplo1
1093 """
1094 #Setting the signal
1095 fs = 8000
1096 f0 = 200
1097 f1 = 400
1098 T = 1./fs
1099 x = numpy.arange(160)
1100 y1 = numpy.sin(2*numpy.pi*f0*x*T)
1101 y2 = numpy.sin(2*numpy.pi*f1*x*T)
1102 signalList = [y1,y2]
1103
1104 xmin = numpy.min(x)
1105 xmax = numpy.max(x)
1106 ymin = numpy.min(y1)
1107 ymax = numpy.max(y1)
1108
1109 # Creating Object
1110 indexPlot = 1
1111 nsubplot = 2
1112 winTitle = "mi grafico v1"
1113
1114 subplotTitle = "subplot - No."
1115 xlabel = ""
1116 ylabel = ""
1117
1118 linearObj = LinearPlot(indexPlot,nsubplot,winTitle)
1119
1120 #Config SubPlots
1121 for subplot in range(nsubplot):
1122 indexplot = subplot + 1
1123 title = subplotTitle + '%d'%indexplot
1124 linearObj.setup(indexplot, xmin, xmax, ymin, ymax, title, xlabel, ylabel)
1125
1126 #Plotting
1127 type = "simple"
1128 for subplot in range(nsubplot):
1129 indexplot = subplot + 1
1130 y = signalList[subplot]
1131 linearObj.plot(indexplot, x, y, type)
1132
1133 linearObj.refresh()
1134 # linearObj.show()
1135
1136
1137 """
1138 Ejemplo2
1139 """
1140
1141 # make these smaller to increase the resolution
1142 dx, dy = 0.05, 0.05
1143 x = numpy.arange(-3.0, 3.0001, dx)
1144 y = numpy.arange(-2.0, 2.0001, dy)
1145 # X,Y = numpy.meshgrid(x, y)
1146 X,Y = sn.ndgrid(x, y)
1147 Z = (1- X/2 + X**5 + Y**3)*numpy.exp(-X**2-Y**2)
1148
1149 # Creating Object
1150 indexPlot = 2
1151 nsubplot = 1
1152 winTitle = "mi grafico pcolor"
1153 colormap = "br_green"
1154 showColorbar = True
1155 showPowerProfile = False
1156 XAxisAsTime = False
1157
1158 subplotTitle = "subplot no. "
1159 xlabel = ""
1160 ylabel = ""
1161
1162 xmin = -3.0
1163 xmax = 3.0
1164 ymin = -2.0
1165 ymax = 2.0
1166 zmin = -0.3
1167 zmax = 1.0
1168
1169 isPlotIni = False
1170 isPlotConfig = False
1171 ntimes = 10
1172
1173
1174 for i in range(ntimes):
1175
1176 # Instancia del objeto
1177 if not(isPlotConfig):
1178 pcolorObj = PcolorPlot(indexPlot, nsubplot, winTitle, colormap, showColorbar, showPowerProfile, XAxisAsTime)
1179 isPlotConfig = True
1180
1181 # Crea los subplots
1182 if not(isPlotIni):
1183 for index in range(nsubplot):
1184 indexplot = index + 1
1185 title = subplotTitle + '%d'%indexplot
1186 subplot = index
1187 pcolorObj.createObjects(indexplot,xmin,xmax,ymin,ymax,zmin,zmax,title,xlabel,ylabel)
1188 isPlotIni = True
1189
1190 # Inicializa el grafico en cada iteracion
1191 pcolorObj.setFigure(indexPlot)
1192 pcolorObj.setNewPage("")
1193
1194 for index in range(nsubplot):
1195 subplot = index
1196 pcolorObj.iniPlot(subplot+1)
1197
1198 #plotea los datos
1199 for channel in range(nsubplot):
1200 data = Z+0.1*numpy.random.randn(len(x),len(y))
1201 pcolorObj.plot(channel+1, x, y, data)
1202
1203 pcolorObj.refresh()
1204
1205 # pcolorObj.closePage() #descomentar esta linea para mas iteraciones
1206
1207 time.sleep(1)
1208
1209 pcolorObj.show()
1210
1211 print "end" No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now