figure.py
395 lines
| 12.2 KiB
| text/x-python
|
PythonLexer
|
r212 | import os | |
|
r201 | import numpy | |
|
r231 | import time, datetime | |
|
r190 | import mpldriver | |
|
r201 | ||
|
r190 | class Figure: | |
|
r201 | ||
__driver = mpldriver | |||
|
r206 | fig = None | |
|
r201 | ||
idfigure = None | |||
wintitle = None | |||
|
r192 | width = None | |
height = None | |||
|
r201 | nplots = None | |
|
r231 | timerange = None | |
|
r201 | ||
axesObjList = [] | |||
WIDTH = None | |||
HEIGHT = None | |||
|
r212 | PREFIX = 'fig' | |
|
r201 | ||
def __init__(self): | |||
raise ValueError, "This method is not implemented" | |||
|
r206 | def __del__(self): | |
self.__driver.closeFigure() | |||
|
r212 | ||
def getFilename(self, name, ext='.png'): | |||
|
r238 | filename = '%s-%s_%s%s' %(self.wintitle[0:10], self.PREFIX, name, ext) | |
|
r212 | ||
return filename | |||
|
r206 | ||
|
r201 | def getAxesObjList(self): | |
return self.axesObjList | |||
def getSubplots(self): | |||
raise ValueError, "Abstract method: This method should be defined" | |||
|
r210 | def getScreenDim(self, widthplot, heightplot): | |
|
r201 | ||
nrow, ncol = self.getSubplots() | |||
|
r210 | widthscreen = widthplot*ncol | |
heightscreen = heightplot*nrow | |||
|
r201 | ||
|
r210 | return widthscreen, heightscreen | |
|
r230 | ||
def getTimeLim(self, x, xmin, xmax): | |||
thisdatetime = datetime.datetime.fromtimestamp(numpy.min(x)) | |||
thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0)) | |||
#################################################### | |||
#If the x is out of xrange | |||
if xmax < (thisdatetime - thisdate).seconds/(60*60.): | |||
xmin = None | |||
xmax = None | |||
if xmin == None: | |||
td = thisdatetime - thisdate | |||
xmin = td.seconds/(60*60.) | |||
if xmax == None: | |||
|
r231 | xmax = xmin + self.timerange/(60*60.) | |
|
r201 | ||
|
r230 | mindt = thisdate + datetime.timedelta(0,0,0,0,0, xmin) | |
tmin = time.mktime(mindt.timetuple()) | |||
maxdt = thisdate + datetime.timedelta(0,0,0,0,0, xmax) | |||
tmax = time.mktime(maxdt.timetuple()) | |||
|
r231 | self.timerange = tmax - tmin | |
|
r230 | ||
return tmin, tmax | |||
|
r201 | def init(self, idfigure, nplots, wintitle): | |
|
r204 | ||
raise ValueError, "This method has been replaced with createFigure" | |||
|
r210 | def createFigure(self, idfigure, wintitle, widthplot=None, heightplot=None): | |
|
r201 | ||
""" | |||
|
r204 | Crea la figura de acuerdo al driver y parametros seleccionados seleccionados. | |
Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH | |||
y self.HEIGHT y el numero de subplots (nrow, ncol) | |||
|
r201 | Input: | |
|
r204 | idfigure : Los parametros necesarios son | |
wintitle : | |||
|
r201 | """ | |
|
r190 | ||
|
r210 | if widthplot == None: | |
widthplot = self.WIDTH | |||
if heightplot == None: | |||
heightplot = self.HEIGHT | |||
|
r190 | self.idfigure = idfigure | |
|
r201 | ||
self.wintitle = wintitle | |||
|
r210 | self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot) | |
|
r201 | ||
self.fig = self.__driver.createFigure(self.idfigure, | |||
self.wintitle, | |||
|
r210 | self.widthscreen, | |
self.heightscreen) | |||
|
r201 | ||
self.axesObjList = [] | |||
|
r190 | ||
|
r201 | def setDriver(self, driver=mpldriver): | |
self.__driver = driver | |||
|
r190 | def setTitle(self, title): | |
|
r201 | ||
self.__driver.setTitle(self.fig, title) | |||
|
r190 | ||
|
r201 | def setWinTitle(self, title): | |
self.__driver.setWinTitle(self.fig, title=title) | |||
|
r196 | ||
|
r201 | def setTextFromAxes(self, text): | |
raise ValueError, "Este metodo ha sido reemplazaado con el metodo setText de la clase Axes" | |||
|
r190 | ||
def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan): | |||
|
r201 | ||
raise ValueError, "Este metodo ha sido reemplazaado con el metodo addAxes" | |||
def addAxes(self, *args): | |||
""" | |||
Input: | |||
*args : Los parametros necesarios son | |||
nrow, ncol, xpos, ypos, colspan, rowspan | |||
""" | |||
axesObj = Axes(self.fig, *args) | |||
self.axesObjList.append(axesObj) | |||
|
r190 | ||
|
r212 | def saveFigure(self, figpath, figfile, *args): | |
filename = os.path.join(figpath, figfile) | |||
self.__driver.saveFigure(self.fig, filename, *args) | |||
|
r209 | ||
|
r190 | def draw(self): | |
|
r201 | ||
self.__driver.draw(self.fig) | |||
|
r190 | ||
def run(self): | |||
|
r201 | ||
raise ValueError, "This method is not implemented" | |||
axesList = property(getAxesObjList) | |||
|
r190 | ||
class Axes: | |||
|
r201 | ||
__driver = mpldriver | |||
fig = None | |||
|
r190 | ax = None | |
|
r201 | plot = None | |
|
r190 | ||
|
r210 | __firsttime = None | |
|
r201 | ||
|
r204 | __showprofile = False | |
|
r210 | xmin = None | |
xmax = None | |||
ymin = None | |||
ymax = None | |||
zmin = None | |||
zmax = None | |||
|
r207 | ||
|
r201 | def __init__(self, *args): | |
""" | |||
Input: | |||
*args : Los parametros necesarios son | |||
fig, nrow, ncol, xpos, ypos, colspan, rowspan | |||
""" | |||
ax = self.__driver.createAxes(*args) | |||
self.fig = args[0] | |||
|
r190 | self.ax = ax | |
|
r201 | self.plot = None | |
|
r210 | self.__firsttime = True | |
|
r239 | self.idlineList = [] | |
|
r201 | ||
def setText(self, text): | |||
self.__driver.setAxesText(self.ax, text) | |||
|
r209 | def setXAxisAsTime(self): | |
pass | |||
|
r201 | def pline(self, x, y, | |
xmin=None, xmax=None, | |||
ymin=None, ymax=None, | |||
xlabel='', ylabel='', | |||
title='', | |||
**kwargs): | |||
""" | |||
Input: | |||
x : | |||
y : | |||
xmin : | |||
xmax : | |||
ymin : | |||
ymax : | |||
xlabel : | |||
ylabel : | |||
title : | |||
**kwargs : Los parametros aceptados son | |||
ticksize | |||
|
r204 | ytick_visible | |
|
r201 | """ | |
|
r210 | if self.__firsttime: | |
|
r201 | ||
if xmin == None: xmin = numpy.nanmin(x) | |||
if xmax == None: xmax = numpy.nanmax(x) | |||
if ymin == None: ymin = numpy.nanmin(y) | |||
if ymax == None: ymax = numpy.nanmax(y) | |||
self.plot = self.__driver.createPline(self.ax, x, y, | |||
xmin, xmax, | |||
ymin, ymax, | |||
xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title, | |||
**kwargs) | |||
|
r239 | ||
self.idlineList.append(0) | |||
|
r210 | self.__firsttime = False | |
|
r201 | return | |
self.__driver.pline(self.plot, x, y, xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title) | |||
|
r239 | ||
def addpline(self, x, y, idline, **kwargs): | |||
lines = self.ax.lines | |||
if idline in self.idlineList: | |||
self.__driver.set_linedata(self.ax, x, y, idline) | |||
if idline not in(self.idlineList): | |||
self.__driver.addpline(self.ax, x, y, **kwargs) | |||
self.idlineList.append(idline) | |||
return | |||
|
r201 | ||
|
r229 | def pmultiline(self, x, y, | |
xmin=None, xmax=None, | |||
ymin=None, ymax=None, | |||
xlabel='', ylabel='', | |||
title='', | |||
**kwargs): | |||
if self.__firsttime: | |||
if xmin == None: xmin = numpy.nanmin(x) | |||
if xmax == None: xmax = numpy.nanmax(x) | |||
if ymin == None: ymin = numpy.nanmin(y) | |||
if ymax == None: ymax = numpy.nanmax(y) | |||
self.plot = self.__driver.createPmultiline(self.ax, x, y, | |||
xmin, xmax, | |||
ymin, ymax, | |||
xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title, | |||
**kwargs) | |||
self.__firsttime = False | |||
return | |||
self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title) | |||
|
r240 | ||
def pmultilineyaxis(self, x, y, | |||
xmin=None, xmax=None, | |||
ymin=None, ymax=None, | |||
xlabel='', ylabel='', | |||
title='', | |||
**kwargs): | |||
|
r201 | ||
|
r240 | if self.__firsttime: | |
if xmin == None: xmin = numpy.nanmin(x) | |||
if xmax == None: xmax = numpy.nanmax(x) | |||
if ymin == None: ymin = numpy.nanmin(y) | |||
if ymax == None: ymax = numpy.nanmax(y) | |||
self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y, | |||
xmin, xmax, | |||
ymin, ymax, | |||
xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title, | |||
**kwargs) | |||
self.__firsttime = False | |||
return | |||
self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title) | |||
|
r201 | def pcolor(self, x, y, z, | |
xmin=None, xmax=None, | |||
ymin=None, ymax=None, | |||
zmin=None, zmax=None, | |||
xlabel='', ylabel='', | |||
|
r229 | title='', rti = False, colormap='jet', | |
|
r201 | **kwargs): | |
""" | |||
Input: | |||
x : | |||
y : | |||
x : | |||
xmin : | |||
xmax : | |||
ymin : | |||
ymax : | |||
zmin : | |||
zmax : | |||
xlabel : | |||
ylabel : | |||
title : | |||
**kwargs : Los parametros aceptados son | |||
ticksize=9, | |||
cblabel='' | |||
|
r207 | rti = True or False | |
|
r201 | """ | |
|
r210 | if self.__firsttime: | |
|
r201 | ||
if xmin == None: xmin = numpy.nanmin(x) | |||
if xmax == None: xmax = numpy.nanmax(x) | |||
if ymin == None: ymin = numpy.nanmin(y) | |||
if ymax == None: ymax = numpy.nanmax(y) | |||
if zmin == None: zmin = numpy.nanmin(z) | |||
if zmax == None: zmax = numpy.nanmax(z) | |||
|
r207 | ||
|
r201 | self.plot = self.__driver.createPcolor(self.ax, x, y, z, | |
xmin, xmax, | |||
ymin, ymax, | |||
zmin, zmax, | |||
xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title, | |||
|
r229 | colormap=colormap, | |
|
r201 | **kwargs) | |
|
r210 | ||
if self.xmin == None: self.xmin = xmin | |||
if self.xmax == None: self.xmax = xmax | |||
if self.ymin == None: self.ymin = ymin | |||
if self.ymax == None: self.ymax = ymax | |||
if self.zmin == None: self.zmin = zmin | |||
if self.zmax == None: self.zmax = zmax | |||
self.__firsttime = False | |||
|
r201 | return | |
|
r207 | ||
if rti: | |||
|
r210 | self.__driver.addpcolor(self.ax, x, y, z, self.zmin, self.zmax, | |
xlabel=xlabel, | |||
ylabel=ylabel, | |||
|
r229 | title=title, | |
colormap=colormap) | |||
|
r207 | return | |
|
r210 | self.__driver.pcolor(self.plot, z, | |
xlabel=xlabel, | |||
ylabel=ylabel, | |||
title=title) | |||
|
r207 | ||