schainPlotLib.py
515 lines
| 16.4 KiB
| text/x-python
|
PythonLexer
|
r133 | import plplot | |
import numpy | |||
import sys | |||
import plplot #condicional | |||
|
r150 | import matplotlib as mpl | |
#mpl.use('TKAgg') | |||
import matplotlib.pyplot as plt | |||
|
r133 | class Driver: | |
|
r138 | def __init__(self,driver, idfigure, xw, yw, wintitle, overplot, colormap, colorbar): | |
|
r133 | if driver == "plplot": | |
|
r138 | self.driver = PlplotDriver(idfigure, xw, yw, wintitle, overplot, colormap, colorbar) | |
|
r133 | elif driver == "mpl": | |
|
r138 | self.driver = MplDriver(idfigure, xw, yw, wintitle, overplot, colormap, colorbar) | |
|
r133 | else: | |
raise ValueError, "The driver: %s is not defined"%driver | |||
class PlplotDriver: | |||
__isDriverOpen = False | |||
pldriver = None | |||
|
r139 | __xg = None | |
__yg = None | |||
|
r138 | def __init__(self, idfigure, xw, yw, wintitle, overplot, colormap, colorbar): | |
|
r133 | ||
if idfigure == None: | |||
raise ValueError, 'idfigure input must be defined' | |||
self.idfigure = idfigure | |||
self.xw = xw | |||
self.yw = yw | |||
self.wintitle = wintitle | |||
self.overplot = overplot | |||
self.colormap = colormap | |||
|
r138 | self.colorbar = colorbar | |
|
r133 | ||
|
r135 | def setFigure(self): | |
|
r133 | """ | |
previous configuration to open(init) the plplot driver | |||
""" | |||
plplot.plsstrm(self.idfigure) | |||
plplot.plparseopts([self.wintitle],plplot.PL_PARSE_FULL) | |||
plplot.plsetopt("geometry", "%dx%d"%(self.xw, self.yw)) | |||
|
r134 | ||
|
r153 | def selectFigure(self): | |
plplot.plsstrm(self.idfigure) | |||
|
r133 | ||
|
r161 | ||
|
r133 | def openDriver(self, pldriver=None): | |
if pldriver == None: | |||
if sys.platform == "linux": | |||
pldriver = "xcairo" | |||
|
r134 | ||
if sys.platform == "linux2": | |||
pldriver = "xcairo" | |||
|
r133 | elif sys.platform == "darwin": | |
pldriver = "xwin" | |||
else: | |||
pldriver = "" | |||
|
r134 | plplot.plsdev("xwin") #para pruebas | |
plplot.plscolbg(255,255,255) | |||
plplot.plscol0(1,0,0,0) | |||
|
r133 | plplot.plinit() | |
plplot.plspause(False) | |||
self.pldriver = pldriver | |||
def closeDriver(self): | |||
pass | |||
def openPage(self): | |||
plplot.plbop() | |||
|
r161 | #plplot.pladv(0) | |
|
r133 | ||
def closePage(self): | |||
plplot.pleop() | |||
def openFigure(self): | |||
plplot.plbop() | |||
plplot.pladv(0) | |||
def closeFigure(self): | |||
plplot.pleop() | |||
def setSubPlots(self,nrows, ncolumns): | |||
|
r161 | plplot.plssub(ncolumns, nrows) | |
|
r133 | ||
|
r150 | def setPlotLabels(self, id, xlabel, ylabel, title): | |
|
r135 | plplot.pllab(xlabel, ylabel, title) | |
|
r133 | ||
|
r135 | def setFigTitle(self, title,color="black", szchar=0.55): | |
|
r161 | self.setSubPlots(1, 1) | |
|
r135 | plplot.pladv(0) | |
plplot.plvpor(0., 1., 0., 1.) | |||
|
r133 | ||
|
r135 | if color == "black": | |
plplot.plcol0(1) | |||
if color == "white": | |||
plplot.plcol0(15) | |||
|
r133 | ||
|
r135 | plplot.plschr(0.0,szchar) | |
plplot.plmtex("t",-1., 0.5, 0.5, title) | |||
|
r139 | def plotColorbar(self, minvalue, maxvalue, xpos, ypos): | |
|
r138 | # plplot.pladv(id) | |
# plplot.plschr(0.0,szchar-0.05) | |||
xmin = 0; xmax = 1 | |||
ymin = minvalue; ymax = maxvalue | |||
plplot.plvpor(xpos[0], xpos[1], ypos[0], ypos[1]) | |||
plplot.plwind(float(xmin), float(xmax), float(ymin), float(ymax)) | |||
plplot.plbox("bc", 0.0, 0, "bcmtsv", 0.0, 0) | |||
data = numpy.arange(256) | |||
data = numpy.reshape(data, (1,-1)) | |||
plplot.plimage(data, | |||
float(xmin), | |||
float(xmax), | |||
float(ymin), | |||
float(ymax), | |||
0., | |||
255., | |||
float(xmin), | |||
float(xmax), | |||
float(ymin), | |||
float(ymax)) | |||
|
r139 | ||
def __getGrid(self, x, y, deltax=None, deltay=None): | |||
if not(len(x)>0 and len(y)>0): | |||
raise ValueError, "x axis and y axis are empty" | |||
if deltax == None: deltax = x[-1] - x[-2] | |||
if deltay == None: deltay = y[-1] - y[-2] | |||
x1 = numpy.append(x, x[-1] + deltax) | |||
y1 = numpy.append(y, y[-1] + deltay) | |||
xg = (numpy.multiply.outer(x1, numpy.ones(len(y1)))) | |||
yg = (numpy.multiply.outer(numpy.ones(len(x1)), y1)) | |||
return xg, yg | |||
def pcolor(self, id, xpos, ypos, data, x, y, xmin, xmax, ymin, ymax, zmin, zmax, deltax=None, deltay=None, getGrid=True, xaxisastime = False, timefmt="%H:%M"): | |||
plplot.pladv(id) | |||
plplot.plvpor(xpos[0], xpos[1], ypos[0], ypos[1]) | |||
plplot.plwind(float(xmin),float(xmax), float(ymin), float(ymax)) | |||
|
r138 | ||
|
r139 | if xaxisastime: | |
timedelta = (xmax - xmin + 1)/8. | |||
if getGrid: | |||
self.__xg, self.__yg = self.__getGrid(x, y, deltax, deltay) | |||
|
r141 | if deltax == None: deltax = x[-1] - x[0] | |
# if deltay == None: deltay = y[-1] - y[-2] | |||
|
r139 | xmin = x[0] | |
xmax = xmin + deltax | |||
plplot.plimagefr(data, | |||
float(xmin), | |||
float(xmax), | |||
float(ymin), | |||
float(ymax), | |||
0., | |||
0., | |||
float(zmin), | |||
float(zmax), | |||
plplot.pltr2, | |||
self.__xg, | |||
self.__yg) | |||
if xaxisastime: | |||
plplot.pltimefmt(timefmt) | |||
xopt = "bcstd" | |||
yopt = "bcst" | |||
plplot.plbox(xopt, timedelta, 3, yopt, 0.0, 0) | |||
else: | |||
xopt = "bcst" | |||
yopt = "bcst" | |||
plplot.plbox(xopt, 0.0, 0, yopt, 0.0, 0) | |||
|
r138 | ||
|
r135 | def plotBox(self, id, xpos, ypos, xmin, xmax, ymin, ymax, minvalue, maxvalue, xopt, yopt, szchar=0.6, xaxisastime = False, timefmt="%H:%M"): | |
""" | |||
xopt, yopt: entradas que no se aplican en MPL | |||
""" | |||
plplot.pladv(id) | |||
plplot.plschr(0.0,szchar-0.05) | |||
plplot.plvpor(xpos[0], xpos[1], ypos[0], ypos[1]) | |||
plplot.plwind(float(xmin), float(xmax), float(ymin), float(ymax)) | |||
if xaxisastime: | |||
plplot.pltimefmt(timefmt) | |||
timedelta = (xmax - xmin + 1)/8. | |||
plplot.plbox(xopt, timedelta, 3, yopt, 0.0, 0) | |||
else: | |||
plplot.plbox(xopt, 0.0, 0, yopt, 0.0, 0) | |||
|
r133 | ||
|
r135 | def refresh(self): | |
plplot.plflush() | |||
|
r150 | ||
def show(self): | |||
plplot.plspause(True) | |||
plplot.plend() | |||
|
r133 | ||
|
r161 | def basicLine(self, id, xpos, ypos, x, y, xmin, xmax, ymin, ymax, color, xopt="bcst", yopt="bcst"): | |
|
r133 | ||
|
r135 | """ | |
Inputs: | |||
x: datos en el eje x | |||
|
r133 | ||
|
r135 | y: datos en el eje y | |
|
r133 | ||
|
r135 | xmin, xmax: intervalo de datos en el eje x | |
|
r133 | ||
|
r135 | ymin, ymax: intervalo de datos en el eje y | |
|
r133 | ||
|
r135 | color: color de la linea a dibujarse | |
|
r133 | ||
|
r135 | id: identificador del plot, en este caso indica al frame que pertenece, la posicion de cada | |
plot esta definido por xpos, ypos. | |||
|
r133 | ||
|
r135 | xpos,ypos: coordenadas que indican la posicion del plot en el frame | |
|
r133 | ||
|
r135 | """ | |
|
r133 | ||
|
r135 | plplot.pladv(id) | |
plplot.plvpor(xpos[0], xpos[1], ypos[0], ypos[1]) | |||
plplot.plwind(float(xmin),float(xmax), float(ymin), float(ymax)) | |||
|
r133 | ||
|
r135 | if color == "blue": | |
colline = 9 | |||
if color == "green": | |||
colline = 3 | |||
plplot.plcol0(colline) | |||
plplot.plline(x, y) | |||
|
r133 | plplot.plcol0(1) | |
|
r161 | # plplot.plbox(xopt, 0.0, 0, yopt, 0.0, 0) | |
|
r139 | ||
|
r150 | def save(self, filename): | |
curr_strm = plplot.plgstrm() | |||
save_strm = plplot.plmkstrm() | |||
plplot.plsetopt("geometry", "%dx%d"%(self.xw,self.yw)) | |||
plplot.plsdev("png") | |||
plplot.plsfnam(filename) | |||
plplot.plcpstrm(curr_strm,0) | |||
plplot.plreplot() | |||
plplot.plend1() | |||
plplot.plsstrm(curr_strm) | |||
|
r139 | def setColormap(self, colormap="gray"): | |
if colormap == None: | |||
return | |||
ncolor = None | |||
rgb_lvl = None | |||
# Routine for defining a specific color map 1 in HLS space. | |||
# if gray is true, use basic grayscale variation from half-dark to light. | |||
# otherwise use false color variation from blue (240 deg) to red (360 deg). | |||
# Independent variable of control points. | |||
i = numpy.array((0., 1.)) | |||
if colormap=="gray": | |||
ncolor = 256 | |||
# Hue for control points. Doesn't matter since saturation is zero. | |||
h = numpy.array((0., 0.)) | |||
# Lightness ranging from half-dark (for interest) to light. | |||
l = numpy.array((0.5, 1.)) | |||
# Gray scale has zero saturation | |||
s = numpy.array((0., 0.)) | |||
# number of cmap1 colours is 256 in this case. | |||
plplot.plscmap1n(ncolor) | |||
# Interpolate between control points to set up cmap1. | |||
plplot.plscmap1l(0, i, h, l, s) | |||
return None | |||
if colormap == 'jet': | |||
ncolor = 256 | |||
pos = numpy.zeros((ncolor)) | |||
r = numpy.zeros((ncolor)) | |||
g = numpy.zeros((ncolor)) | |||
b = numpy.zeros((ncolor)) | |||
for i in range(ncolor): | |||
if(i <= 35.0/100*(ncolor-1)): rf = 0.0 | |||
elif (i <= 66.0/100*(ncolor-1)): rf = (100.0/31)*i/(ncolor-1) - 35.0/31 | |||
elif (i <= 89.0/100*(ncolor-1)): rf = 1.0 | |||
else: rf = (-100.0/22)*i/(ncolor-1) + 111.0/22 | |||
if(i <= 12.0/100*(ncolor-1)): gf = 0.0 | |||
elif(i <= 38.0/100*(ncolor-1)): gf = (100.0/26)*i/(ncolor-1) - 12.0/26 | |||
elif(i <= 64.0/100*(ncolor-1)): gf = 1.0 | |||
elif(i <= 91.0/100*(ncolor-1)): gf = (-100.0/27)*i/(ncolor-1) + 91.0/27 | |||
else: gf = 0.0 | |||
if(i <= 11.0/100*(ncolor-1)): bf = (50.0/11)*i/(ncolor-1) + 0.5 | |||
elif(i <= 34.0/100*(ncolor-1)): bf = 1.0 | |||
elif(i <= 65.0/100*(ncolor-1)): bf = (-100.0/31)*i/(ncolor-1) + 65.0/31 | |||
else: bf = 0 | |||
r[i] = rf | |||
g[i] = gf | |||
b[i] = bf | |||
pos[i] = float(i)/float(ncolor-1) | |||
plplot.plscmap1n(ncolor) | |||
plplot.plscmap1l(1, pos, r, g, b) | |||
if colormap=="br_green": | |||
ncolor = 256 | |||
# Hue ranges from blue (240 deg) to red (0 or 360 deg) | |||
h = numpy.array((240., 0.)) | |||
# Lightness and saturation are constant (values taken from C example). | |||
l = numpy.array((0.6, 0.6)) | |||
s = numpy.array((0.8, 0.8)) | |||
# number of cmap1 colours is 256 in this case. | |||
plplot.plscmap1n(ncolor) | |||
# Interpolate between control points to set up cmap1. | |||
plplot.plscmap1l(0, i, h, l, s) | |||
return None | |||
if colormap=="tricolor": | |||
ncolor = 3 | |||
# Hue ranges from blue (240 deg) to red (0 or 360 deg) | |||
h = numpy.array((240., 0.)) | |||
# Lightness and saturation are constant (values taken from C example). | |||
l = numpy.array((0.6, 0.6)) | |||
s = numpy.array((0.8, 0.8)) | |||
# number of cmap1 colours is 256 in this case. | |||
plplot.plscmap1n(ncolor) | |||
# Interpolate between control points to set up cmap1. | |||
plplot.plscmap1l(0, i, h, l, s) | |||
return None | |||
if colormap == 'rgb' or colormap == 'rgb666': | |||
color_sz = 6 | |||
ncolor = color_sz*color_sz*color_sz | |||
pos = numpy.zeros((ncolor)) | |||
r = numpy.zeros((ncolor)) | |||
g = numpy.zeros((ncolor)) | |||
b = numpy.zeros((ncolor)) | |||
ind = 0 | |||
for ri in range(color_sz): | |||
for gi in range(color_sz): | |||
for bi in range(color_sz): | |||
r[ind] = ri/(color_sz-1.0) | |||
g[ind] = gi/(color_sz-1.0) | |||
b[ind] = bi/(color_sz-1.0) | |||
pos[ind] = ind/(ncolor-1.0) | |||
ind += 1 | |||
rgb_lvl = [6,6,6] #Levels for RGB colors | |||
if colormap == 'rgb676': | |||
ncolor = 6*7*6 | |||
pos = numpy.zeros((ncolor)) | |||
r = numpy.zeros((ncolor)) | |||
g = numpy.zeros((ncolor)) | |||
b = numpy.zeros((ncolor)) | |||
ind = 0 | |||
for ri in range(8): | |||
for gi in range(8): | |||
for bi in range(4): | |||
r[ind] = ri/(6-1.0) | |||
g[ind] = gi/(7-1.0) | |||
b[ind] = bi/(6-1.0) | |||
pos[ind] = ind/(ncolor-1.0) | |||
ind += 1 | |||
rgb_lvl = [6,7,6] #Levels for RGB colors | |||
if colormap == 'rgb685': | |||
ncolor = 6*8*5 | |||
pos = numpy.zeros((ncolor)) | |||
r = numpy.zeros((ncolor)) | |||
g = numpy.zeros((ncolor)) | |||
b = numpy.zeros((ncolor)) | |||
ind = 0 | |||
for ri in range(8): | |||
for gi in range(8): | |||
for bi in range(4): | |||
r[ind] = ri/(6-1.0) | |||
g[ind] = gi/(8-1.0) | |||
b[ind] = bi/(5-1.0) | |||
pos[ind] = ind/(ncolor-1.0) | |||
ind += 1 | |||
rgb_lvl = [6,8,5] #Levels for RGB colors | |||
if colormap == 'rgb884': | |||
ncolor = 8*8*4 | |||
pos = numpy.zeros((ncolor)) | |||
r = numpy.zeros((ncolor)) | |||
g = numpy.zeros((ncolor)) | |||
b = numpy.zeros((ncolor)) | |||
ind = 0 | |||
for ri in range(8): | |||
for gi in range(8): | |||
for bi in range(4): | |||
r[ind] = ri/(8-1.0) | |||
g[ind] = gi/(8-1.0) | |||
b[ind] = bi/(4-1.0) | |||
pos[ind] = ind/(ncolor-1.0) | |||
ind += 1 | |||
rgb_lvl = [8,8,4] #Levels for RGB colors | |||
if ncolor == None: | |||
|
r153 | raise ValueError, "The colormap selected (%s) is not valid" %(colormap) | |
|
r139 | ||
plplot.plscmap1n(ncolor) | |||
plplot.plscmap1l(1, pos, r, g, b) | |||
return rgb_lvl | |||
|
r138 | ||
class MplDriver: | |||
|
r150 | # fig = None | |
nrows = None | |||
ncolumns = None | |||
__axesId = None | |||
__setData = None | |||
def __init__(self, idfigure, xw, yw, wintitle, overplot, colormap, colorbar): | |||
if idfigure == None: | |||
raise ValueError, 'idfigure input must be defined' | |||
self.idfigure = idfigure | |||
self.xw = xw | |||
self.yw = yw | |||
self.wintitle = wintitle | |||
self.overplot = overplot | |||
self.colormap = colormap | |||
self.colorbar = colorbar | |||
self.__axesId = {} | |||
def setFigure(self): | |||
plt.ioff() | |||
fig = plt.figure(self.idfigure) | |||
fig.canvas.manager.set_window_title(self.wintitle) | |||
fig.canvas.resize(self.xw,self.yw) | |||
plt.ion() | |||
def setColormap(self, colormap): | |||
pass | |||
def openDriver(self): | |||
pass | |||
def openFigure(self): | |||
pass | |||
def setFigTitle(self,title): | |||
fig = plt.figure(self.idfigure) | |||
fig.suptitle(title,fontsize=11, fontweight='bold') | |||
def setSubPlots(self,nrows,ncolumns): | |||
fig = plt.figure(self.idfigure) | |||
self.nrows = nrows | |||
self.ncolumns = ncolumns | |||
#self.__axesId = fig.add_subplot(nrows,ncolumns) | |||
def plotBox(self, id, xpos, ypos, xmin, xmax, ymin, ymax, minvalue, maxvalue, xopt, yopt, szchar, xaxisastime, timefmt): | |||
fig = plt.figure(self.idfigure) | |||
ax = fig.add_subplot(self.nrows,self.ncolumns,id) | |||
ax.set_xlim([xmin,xmax]) | |||
ax.set_ylim([ymin,ymax]) | |||
self.__axesId.setdefault(id) | |||
self.__axesId[id] = ax | |||
def setPlotLabels(self, id, xlabel, ylabel, title): | |||
ax = self.__axesId[id] | |||
ax.set_xlabel(xlabel) | |||
ax.set_ylabel(ylabel) | |||
ax.set_title(title) | |||
def basicLine(self, id, xpos, ypos, x, y, xmin, xmax, ymin, ymax, color): | |||
ax = self.__axesId[id] | |||
if self.__setData == None: | |||
ax.plot(x,y,color) | |||
self.__setData = True | |||
else: | |||
ax.lines[0].set_data(x,y) | |||
def refresh(self): | |||
plt.draw() | |||
def plotColorbar(self): | |||
pass | |||
def closePage(self): | |||
|
r138 | pass |