Small change: now possible to submit figure and axis to plot on

master
per.andreas.brodtkorb 13 years ago
parent 412990574f
commit 0c2f6621e4

@ -20,30 +20,40 @@ def _matchfun(x, gidtxt):
return x.get_gid() == gidtxt return x.get_gid() == gidtxt
return False return False
def delete_object(gidtxt, cf=None, ca=None, verbose=False): def delete_text_object(gidtxt, figure=None, axis=None, verbose=False):
''' '''
Delete all objects matching the gidtxt if it exists Delete all text objects matching the gidtxt if it exists
Parameters
----------
gidtxt : string
figure, axis : objects
current figure and current axis, respectively.
verbose : bool
If true print warnings when trying to delete non-existent objects
''' '''
if cf is None: if figure is None:
cf = plotbackend.gcf() figure = plotbackend.gcf()
if ca is None: if axis is None:
ca = plotbackend.gca() axis = figure.gca()
lmatchfun = lambda x : _matchfun(x, gidtxt) lmatchfun = lambda x : _matchfun(x, gidtxt)
objs = plotbackend.findobj(cf, lmatchfun) objs = axis.findobj(lmatchfun)
if len(objs): for obj in objs:
for i in objs:
try: try:
ca.texts.remove(i) axis.texts.remove(obj)
except: except:
if verbose: if verbose:
warnings.warn('Tried to delete a non-existing %s from axes' % gidtxt) warnings.warn('Tried to delete a non-existing %s from axis' % gidtxt)
objs = figure.findobj(lmatchfun)
for obj in objs:
try: try:
cf.texts.remove(i) figure.texts.remove(obj)
except: except:
if verbose: if verbose:
warnings.warn('Tried to delete a non-existing %s from figure' % gidtxt) warnings.warn('Tried to delete a non-existing %s from figure' % gidtxt)
def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0): def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0, figure=None, axis=None):
''' '''
Places contour level text in the current window Places contour level text in the current window
@ -58,6 +68,10 @@ def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0):
contour line encloses contour line encloses
n : integer n : integer
maximum N digits of precision (default 4) maximum N digits of precision (default 4)
figure, axis : objects
current figure and current axis, respectively.
default figure = plotbackend.gcf(),
axis = plotbackend.gca()
Returns Returns
------- -------
@ -89,10 +103,15 @@ def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0):
>>> plt.show() >>> plt.show()
''' '''
# TODO : Make it work like legend does (but without the box): include position options etc... # TODO : Make it work like legend does (but without the box): include position options etc...
if figure is None:
figure = plotbackend.gcf()
if axis is None:
axis = figure.gca()
clevels = np.atleast_1d(levels) clevels = np.atleast_1d(levels)
cax = plotbackend.gca()
axpos = cax.get_position() axpos = axis.get_position()
xint = axpos.intervalx xint = axpos.intervalx
yint = axpos.intervaly yint = axpos.intervaly
@ -100,7 +119,7 @@ def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0):
yss = yint[0] + ys * (yint[1] - yint[0]) yss = yint[0] + ys * (yint[1] - yint[0])
# delete cltext object if it exists # delete cltext object if it exists
delete_object(_CLTEXT_GID, ca=cax) delete_text_object(_CLTEXT_GID, axis=axis)
charHeight = 1.0 / 33.0 charHeight = 1.0 / 33.0
delta_y = charHeight delta_y = charHeight
@ -115,15 +134,16 @@ def cltext(levels, percent=False, n=4, xs=0.036, ys=0.94, zs=0):
cltxt = ''.join([format_ % level for level in clevels.tolist()]) cltxt = ''.join([format_ % level for level in clevels.tolist()])
titleProp = dict(gid=_CLTEXT_GID, horizontalalignment='left', titleProp = dict(gid=_CLTEXT_GID, horizontalalignment='left',
verticalalignment='center', fontweight='bold', axes=cax) # verticalalignment='center', fontweight='bold', axes=axis) #
ha1 = plotbackend.figtext(xss, yss, titletxt, **titleProp)
ha1 = figure.text(xss, yss, titletxt, **titleProp)
yss -= delta_y; yss -= delta_y;
txtProp = dict(gid=_CLTEXT_GID, horizontalalignment='left', txtProp = dict(gid=_CLTEXT_GID, horizontalalignment='left',
verticalalignment='top', axes=cax) verticalalignment='top', axes=axis)
ha2 = plotbackend.figtext(xss, yss, cltxt, **txtProp)
ha2 = figure.text(xss, yss, cltxt, **txtProp)
plotbackend.draw_if_interactive()
return ha1, ha2 return ha1, ha2
def tallibing(x, y, n, **kwds): def tallibing(x, y, n, **kwds):
@ -149,8 +169,8 @@ def tallibing(x, y, n, **kwds):
>>> import wafo.graphutil as wg >>> import wafo.graphutil as wg
>>> import wafo.demos as wd >>> import wafo.demos as wd
>>> [x,y,z] = wd.peaks(n=20) >>> [x,y,z] = wd.peaks(n=20)
>>> wg.epcolor(x,y,z) >>> h0 = wg.epcolor(x,y,z)
>>> wg.tallibing(x,y,z) >>> h1 = wg.tallibing(x,y,z)
pcolor(x,y,z); shading interp; pcolor(x,y,z); shading interp;
@ -158,28 +178,32 @@ def tallibing(x, y, n, **kwds):
-------- --------
text text
''' '''
axis = kwds.pop('axis',None)
if axis is None:
axis = plotbackend.gca()
x, y, n = np.atleast_1d(x, y, n) x, y, n = np.atleast_1d(x, y, n)
if mlab.isvector(x) or mlab.isvector(y): if mlab.isvector(x) or mlab.isvector(y):
x, y = np.meshgrid(x,y) x, y = np.meshgrid(x,y)
cax = plotbackend.gca()
x = x.ravel() x = x.ravel()
y = y.ravel() y = y.ravel()
n = n.ravel() n = n.ravel()
n = np.round(n) n = np.round(n)
# delete tallibing object if it exists # delete tallibing object if it exists
delete_object(_TALLIBING_GID, ca=cax) delete_text_object(_TALLIBING_GID, axis=axis)
txtProp = dict(gid=_TALLIBING_GID, size=8, color='w', horizontalalignment='center', txtProp = dict(gid=_TALLIBING_GID, size=8, color='w', horizontalalignment='center',
verticalalignment='center', fontweight='demi', axes=cax) verticalalignment='center', fontweight='demi', axes=axis)
txtProp.update(**kwds) txtProp.update(**kwds)
h = [] h = []
for xi,yi, ni in zip(x,y,n): for xi,yi, ni in zip(x,y,n):
if ni: if ni:
h.append(plotbackend.text(xi, yi, str(ni), **txtProp)) h.append(axis.text(xi, yi, str(ni), **txtProp))
plotbackend.draw_if_interactive()
return h return h
def epcolor(*args, **kwds): def epcolor(*args, **kwds):
@ -203,15 +227,20 @@ def epcolor(*args, **kwds):
>>> import wafo.demos as wd >>> import wafo.demos as wd
>>> import wafo.graphutil as wg >>> import wafo.graphutil as wg
>>> x, y, z = wd.peaks(n=20) >>> x, y, z = wd.peaks(n=20)
>>> wg.epcolor(x,y,z) >>> h = wg.epcolor(x,y,z)
See also See also
-------- --------
pylab.pcolor pylab.pcolor
''' '''
axis = kwds.pop('axis',None)
if axis is None:
axis = plotbackend.gca()
midbin = kwds.pop('midbin', True) midbin = kwds.pop('midbin', True)
if not midbin: if not midbin:
return plotbackend.pcolor(*args,**kwds) ret = axis.pcolor(*args,**kwds)
plotbackend.draw_if_interactive()
return ret
nargin = len(args) nargin = len(args)
data = np.atleast_2d(args[-1]).copy() data = np.atleast_2d(args[-1]).copy()
@ -230,7 +259,10 @@ def epcolor(*args, **kwds):
xx = _findbins(x) xx = _findbins(x)
yy = _findbins(y) yy = _findbins(y)
return plotbackend.pcolor(xx, yy, data, **kwds) ret = axis.pcolor(xx, yy, data, **kwds)
plotbackend.draw_if_interactive()
return ret
def _findbins(x): def _findbins(x):
''' Return points half way between all values of X _and_ outside the ''' Return points half way between all values of X _and_ outside the

@ -80,16 +80,20 @@ class WafoData(object):
self.setplotter(kwds.get('plotmethod', None)) self.setplotter(kwds.get('plotmethod', None))
def plot(self, *args, **kwds): def plot(self, *args, **kwds):
axis = kwds.pop('axis',None)
if axis is None:
axis = plotbackend.gca()
tmp = None tmp = None
plotflag = kwds.get('plotflag', None) plotflag = kwds.get('plotflag', None)
if not plotflag and self.children != None: if not plotflag and self.children != None:
plotbackend.hold('on') plotbackend.hold('on')
tmp = [] tmp = []
child_args = args if len(args) else tuple(self.plot_args_children) child_args = kwds.pop('plot_args_children', tuple(self.plot_args_children))
child_kwds = dict(self.plot_kwds_children).copy() child_kwds = dict(self.plot_kwds_children).copy()
child_kwds.update(**kwds) child_kwds.update(kwds.pop('plot_kwds_children', {}))
child_kwds['axis'] = axis
for child in self.children: for child in self.children:
tmp1 = child.plot(*child_args, **kwds) tmp1 = child.plot(*child_args, **child_kwds)
if tmp1 != None: if tmp1 != None:
tmp.append(tmp1) tmp.append(tmp1)
if len(tmp) == 0: if len(tmp) == 0:
@ -97,6 +101,7 @@ class WafoData(object):
main_args = args if len(args) else tuple(self.plot_args) main_args = args if len(args) else tuple(self.plot_args)
main_kwds = dict(self.plot_kwds).copy() main_kwds = dict(self.plot_kwds).copy()
main_kwds.update(kwds) main_kwds.update(kwds)
main_kwds['axis'] = axis
tmp2 = self.plotter.plot(self, *main_args, **main_kwds) tmp2 = self.plotter.plot(self, *main_args, **main_kwds)
return tmp2, tmp return tmp2, tmp
@ -145,11 +150,13 @@ class AxisLabels:
newcopy.__dict__.update(self.__dict__) newcopy.__dict__.update(self.__dict__)
return newcopy return newcopy
def labelfig(self): def labelfig(self, axis=None):
if axis is None:
axis = plotbackend.gca()
try: try:
h1 = plotbackend.title(self.title) h1 = axis.set_title(self.title)
h2 = plotbackend.xlabel(self.xlab) h2 = axis.set_xlabel(self.xlab)
h3 = plotbackend.ylabel(self.ylab) h3 = axis.set_ylabel(self.ylab)
#h4 = plotbackend.zlabel(self.zlab) #h4 = plotbackend.zlabel(self.zlab)
return h1, h2, h3 return h1, h2, h3
except: except:
@ -176,76 +183,85 @@ class Plotter_1d(object):
self.plotfun = None self.plotfun = None
if plotmethod is None: if plotmethod is None:
plotmethod = 'plot' plotmethod = 'plot'
self.plotmethod = plotmethod
self.plotbackend = plotbackend self.plotbackend = plotbackend
try: # try:
self.plotfun = getattr(plotbackend, plotmethod) # self.plotfun = getattr(plotbackend, plotmethod)
except: # except:
pass # pass
def show(self): def show(self):
plotbackend.show() plotbackend.show()
def plot(self, wdata, *args, **kwds): def plot(self, wdata, *args, **kwds):
axis = kwds.pop('axis',None)
if axis is None:
axis = plotbackend.gca()
plotflag = kwds.pop('plotflag', False) plotflag = kwds.pop('plotflag', False)
if plotflag: if plotflag:
h1 = self._plot(plotflag, wdata, *args, **kwds) h1 = self._plot(axis, plotflag, wdata, *args, **kwds)
else: else:
if isinstance(wdata.data, (list, tuple)):
vals = tuple(wdata.data)
else:
vals = (wdata.data,)
if isinstance(wdata.args, (list, tuple)): if isinstance(wdata.args, (list, tuple)):
args1 = tuple((wdata.args)) + (wdata.data,) + args args1 = tuple((wdata.args)) + vals + args
else: else:
args1 = tuple((wdata.args,)) + (wdata.data,) + args args1 = tuple((wdata.args,)) + vals + args
h1 = self.plotfun(*args1, **kwds) plotfun = getattr(axis, self.plotmethod)
h2 = wdata.labels.labelfig() h1 = plotfun(*args1, **kwds)
h2 = wdata.labels.labelfig(axis)
return h1, h2 return h1, h2
def _plot(self, plotflag, wdata, *args, **kwds): def _plot(self, axis, plotflag, wdata, *args, **kwds):
x = wdata.args x = wdata.args
data = transformdata(x, wdata.data, plotflag) data = transformdata(x, wdata.data, plotflag)
dataCI = getattr(wdata, 'dataCI', ()) dataCI = getattr(wdata, 'dataCI', ())
h1 = plot1d(x, data, dataCI, plotflag, *args, **kwds) h1 = plot1d(axis, x, data, dataCI, plotflag, *args, **kwds)
return h1 return h1
def plot1d(args, data, dataCI, plotflag, *varargin, **kwds): def plot1d(axis, args, data, dataCI, plotflag, *varargin, **kwds):
plottype = np.mod(plotflag, 10) plottype = np.mod(plotflag, 10)
if plottype == 0: # % No plotting if plottype == 0: # % No plotting
return [] return []
elif plottype == 1: elif plottype == 1:
H = plotbackend.plot(args, data, *varargin, **kwds) H = axis.plot(args, data, *varargin, **kwds)
elif plottype == 2: elif plottype == 2:
H = plotbackend.step(args, data, *varargin, **kwds) H = axis.step(args, data, *varargin, **kwds)
elif plottype == 3: elif plottype == 3:
H = plotbackend.stem(args, data, *varargin, **kwds) H = axis.stem(args, data, *varargin, **kwds)
elif plottype == 4: elif plottype == 4:
H = plotbackend.errorbar(args, data, dataCI[:,0] - data, dataCI[:,1] - data, *varargin, **kwds) H = axis.errorbar(args, data, yerr=[dataCI[:,0] - data, dataCI[:,1] - data], *varargin, **kwds)
elif plottype == 5: elif plottype == 5:
H = plotbackend.bar(args, data, *varargin, **kwds) H = axis.bar(args, data, *varargin, **kwds)
elif plottype == 6: elif plottype == 6:
level = 0 level = 0
if np.isfinite(level): if np.isfinite(level):
H = plotbackend.fill_between(args, data, level, *varargin, **kwds); H = axis.fill_between(args, data, level, *varargin, **kwds);
else: else:
H = plotbackend.fill_between(args, data, *varargin, **kwds); H = axis.fill_between(args, data, *varargin, **kwds);
elif plottype==7: elif plottype==7:
H = plotbackend.plot(args, data, *varargin, **kwds) H = axis.plot(args, data, *varargin, **kwds)
H = plotbackend.fill_between(args, dataCI[:,0], dataCI[:,1], alpha=0.2, color='r'); H = axis.fill_between(args, dataCI[:,0], dataCI[:,1], alpha=0.2, color='r');
scale = plotscale(plotflag) scale = plotscale(plotflag)
logXscale = 'x' in scale logXscale = 'x' in scale
logYscale = 'y' in scale logYscale = 'y' in scale
logZscale = 'z' in scale logZscale = 'z' in scale
ax = plotbackend.gca()
if logXscale: if logXscale:
plotbackend.setp(ax, xscale='log') axis.set(xscale='log')
if logYscale: if logYscale:
plotbackend.setp(ax, yscale='log') axis.set(yscale='log')
if logZscale: if logZscale:
plotbackend.setp(ax, zscale='log') axis.set(zscale='log')
transFlag = np.mod(plotflag // 10, 10) transFlag = np.mod(plotflag // 10, 10)
logScale = logXscale or logYscale or logZscale logScale = logXscale or logYscale or logZscale
if logScale or (transFlag == 5 and not logScale): if logScale or (transFlag == 5 and not logScale):
ax = list(plotbackend.axis()) ax = list(axis.axis())
fmax1 = data.max() fmax1 = data.max()
if transFlag == 5 and not logScale: if transFlag == 5 and not logScale:
ax[3] = 11 * np.log10(fmax1) ax[3] = 11 * np.log10(fmax1)
@ -254,11 +270,11 @@ def plot1d(args, data, dataCI, plotflag, *varargin, **kwds):
ax[3] = 1.15 * fmax1; ax[3] = 1.15 * fmax1;
ax[2] = ax[3] * 1e-4; ax[2] = ax[3] * 1e-4;
plotbackend.axis(ax) axis.axis(ax)
if np.any(dataCI) and plottype < 3: if np.any(dataCI) and plottype < 3:
plotbackend.hold('on') axis.hold(True)
plot1d(args, dataCI, (), plotflag, 'r--'); plot1d(axis, args, dataCI, (), plotflag, 'r--');
return H return H
def plotscale(plotflag): def plotscale(plotflag):
@ -288,11 +304,26 @@ def plotscale(plotflag):
'yzlog', 'xyzlog' 'yzlog', 'xyzlog'
Example Example
plotscale(100) % xlog >>> for id in range(100,701,100):
plotscale(200) % xlog ... plotscale(id)
plotscale(1000) % ylog 'xlog'
'ylog'
'xylog'
'zlog'
'xzlog'
'yzlog'
'xyzlog'
>>> plotscale(200)
'ylog'
>>> plotscale(300)
'xylog'
>>> plotscale(300)
'xylog'
See also plotscale See also
--------
transformdata
''' '''
scaleId = plotflag // 100 scaleId = plotflag // 100
if scaleId > 7: if scaleId > 7:
@ -341,11 +372,11 @@ class Plotter_2d(Plotter_1d):
plotmethod = 'contour' plotmethod = 'contour'
super(Plotter_2d, self).__init__(plotmethod) super(Plotter_2d, self).__init__(plotmethod)
def _plot(self, plotflag, wdata, *args, **kwds): def _plot(self, axis, plotflag, wdata, *args, **kwds):
h1 = plot2d(wdata, plotflag, *args, **kwds) h1 = plot2d(axis, wdata, plotflag, *args, **kwds)
return h1 return h1
def plot2d(wdata, plotflag, *args, **kwds): def plot2d(axis, wdata, plotflag, *args, **kwds):
f = wdata f = wdata
if isinstance(wdata.args, (list, tuple)): if isinstance(wdata.args, (list, tuple)):
args1 = tuple((wdata.args)) + (wdata.data,) + args args1 = tuple((wdata.args)) + (wdata.data,) + args
@ -365,7 +396,7 @@ def plot2d(wdata, plotflag, *args, **kwds):
clvec = np.sort(CL) clvec = np.sort(CL)
if plotflag in [1, 8, 9]: if plotflag in [1, 8, 9]:
h = plotbackend.contour(*args1, levels=CL, **kwds); h = axis.contour(*args1, levels=CL, **kwds);
#else: #else:
# [cs hcs] = contour3(f.x{:},f.f,CL,sym); # [cs hcs] = contour3(f.x{:},f.f,CL,sym);
@ -378,20 +409,20 @@ def plot2d(wdata, plotflag, *args, **kwds):
clvals = PL[:ncl] if isPL else clvec[:ncl] clvals = PL[:ncl] if isPL else clvec[:ncl]
unused_axcl = cltext(clvals, percent=isPL) # print contour level text unused_axcl = cltext(clvals, percent=isPL) # print contour level text
elif any(plotflag == [7, 9]): elif any(plotflag == [7, 9]):
plotbackend.clabel(h) axis.clabel(h)
else: else:
plotbackend.clabel(h) axis.clabel(h)
elif plotflag == 2: elif plotflag == 2:
h = plotbackend.mesh(*args1, **kwds) h = axis.mesh(*args1, **kwds)
elif plotflag == 3: elif plotflag == 3:
h = plotbackend.surf(*args1, **kwds) #shading interp % flat, faceted % surfc h = axis.surf(*args1, **kwds) #shading interp % flat, faceted % surfc
elif plotflag == 4: elif plotflag == 4:
h = plotbackend.waterfall(*args1, **kwds) h = axis.waterfall(*args1, **kwds)
elif plotflag == 5: elif plotflag == 5:
h = plotbackend.pcolor(*args1, **kwds) #%shading interp % flat, faceted h = axis.pcolor(*args1, **kwds) #%shading interp % flat, faceted
elif plotflag == 10: elif plotflag == 10:
h = plotbackend.contourf(*args1, **kwds) h = axis.contourf(*args1, **kwds)
plotbackend.clabel(h) axis.clabel(h)
plotbackend.colorbar(h) plotbackend.colorbar(h)
else: else:
raise ValueError('unknown option for plotflag') raise ValueError('unknown option for plotflag')

Loading…
Cancel
Save