def delete_text_object(gidtxt, figure=None, axis=None, verbose=False): ''' 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 figure is None: figure = plotbackend.gcf() if axis is None: axis = figure.gca() lmatchfun = lambda x: _matchfun(x, gidtxt) objs = axis.findobj(lmatchfun) for obj in objs: try: axis.texts.remove(obj) except: if verbose: warnings.warn('Tried to delete a non-existing %s from axis' % gidtxt) objs = figure.findobj(lmatchfun) for obj in objs: try: figure.texts.remove(obj) except: if verbose: warnings.warn('Tried to delete a non-existing %s from figure' % gidtxt)
def delete_text_object(gidtxt, figure=None, axis=None, verbose=False): ''' 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 figure is None: figure = plotbackend.gcf() if axis is None: axis = figure.gca() lmatchfun = lambda x: _matchfun(x, gidtxt) objs = axis.findobj(lmatchfun) for obj in objs: try: axis.texts.remove(obj) except: if verbose: warnings.warn( 'Tried to delete a non-existing %s from axis' % gidtxt) objs = figure.findobj(lmatchfun) for obj in objs: try: figure.texts.remove(obj) except: if verbose: 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, figure=None, axis=None): ''' Places contour level text in the current window Parameters ---------- levels : vector contour levels or the corresponding percent which the contour line encloses percent : bool False if levels are the actual contour levels (default) True if levels are the corresponding percent which the contour line encloses n : integer maximum N digits of precision (default 4) figure, axis : objects current figure and current axis, respectively. default figure = plotbackend.gcf(), axis = plotbackend.gca() Returns ------- h = handles to the text objects. Notes ----- CLTEXT creates text objects in the current figure and prints "Level curves at:" if percent is False and "Level curves enclosing:" otherwise and the contour levels or percent. The handles to the lines of text may also be found by h = findobj(gcf,'gid','CLTEXT','type','text'); h = findobj(gca,'gid','CLTEXT','type','text'); To make the text objects follow the data in the axes set the units for the text objects 'data' by set(h,'unit','data') Examples -------- >>> import wafo.graphutil as wg >>> import wafo.demos as wd >>> import pylab as plt >>> x,y,z = wd.peaks(); >>> h = plt.contour(x,y,z) >>> h = wg.cltext(h.levels) >>> plt.show() ''' # 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) axpos = axis.get_position() xint = axpos.intervalx yint = axpos.intervaly xss = xint[0] + xs * (xint[1] - xint[0]) yss = yint[0] + ys * (yint[1] - yint[0]) # delete cltext object if it exists delete_text_object(_CLTEXT_GID, axis=axis) charHeight = 1.0 / 33.0 delta_y = charHeight if percent: titletxt = 'Level curves enclosing:' else: titletxt = 'Level curves at:' format_ = '%0.' + ('%d' % n) + 'g\n' cltxt = ''.join([format_ % level for level in clevels.tolist()]) titleProp = dict(gid=_CLTEXT_GID, horizontalalignment='left', verticalalignment='center', fontweight='bold', axes=axis) ha1 = figure.text(xss, yss, titletxt, **titleProp) yss -= delta_y txtProp = dict(gid=_CLTEXT_GID, horizontalalignment='left', verticalalignment='top', axes=axis) ha2 = figure.text(xss, yss, cltxt, **txtProp) plotbackend.draw_if_interactive() return ha1, ha2