Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapplotter.py

    r2453 r2290  
    4545        if visible is not None:
    4646            self._visible = visible
    47         self._plotter = None
    48         self._inikwg = kwargs
     47        self._plotter = self._newplotter(**kwargs)
     48        # additional tool bar
     49        self._plotter.figmgr.casabar=self._new_custombar()
    4950
    5051        self._panelling = None
     
    5455        self._rows = None
    5556        self._cols = None
     57        self._autoplot = False
    5658        self._minmaxx = None
    5759        self._minmaxy = None
     
    7476        self._panelrows = []
    7577        self._headtext={'string': None, 'textobj': None}
    76         self._colormap = None
    77         self._linestyles = None
    78         self._legendloc = None
    7978
    8079    def _translate(self, instr):
     
    8685        return None
    8786
    88     def _reload_plotter(self):
    89         if self._plotter is not None:
    90             if  not self._plotter.is_dead:
    91                 # clear lines and axes
    92                 self._plotter.clear()
    93             if self.casabar_exists():
    94                 del self._plotter.figmgr.casabar
    95             self._plotter.quit()
    96             del self._plotter
    97         self._plotter = new_asaplot(self._visible,**self._inikwg)
    98         self._plotter.figmgr.casabar=self._new_custombar()
    99         # just to make sure they're set
    100         self._plotter.palette(color=0,colormap=self._colormap,
    101                               linestyle=0,linestyles=self._linestyles)
    102         self._plotter.legend(self._legendloc)
     87    def _newplotter(self, **kwargs):
     88        return new_asaplot(self._visible,**kwargs)
    10389
    10490    def _new_custombar(self):
     
    121107        return False
    122108
    123     def _assert_plotter(self,action="status",errmsg=None):
    124         """
    125         Check plot window status. Returns True if plot window is alive.
    126         Parameters
    127             action:    An action to take if the plotter window is not alive.
    128                        ['status'|'reload'|'halt']
    129                        The action 'status' simply returns False if asaplot
    130                        is not alive. When action='reload', plot window is
    131                        reloaded and the method returns True. Finally, an
    132                        error is raised when action='halt'.
    133             errmsg:    An error (warning) message to send to the logger,
    134                        when plot window is not alive.
    135         """
    136         if self._plotter and not self._plotter.is_dead:
    137             return True
    138         # Plotter is not alive.
    139         haltmsg = "Plotter window has not yet been loaded or is closed."
    140         if type(errmsg)==str and len(errmsg) > 0:
    141             haltmsg = errmsg
    142        
    143         if action.upper().startswith("R"):
    144             # reload plotter
    145             self._reload_plotter()
    146             return True
    147         elif action.upper().startswith("H"):
    148             # halt
    149             asaplog.push(haltmsg)
    150             asaplog.post("ERROR")
    151             raise RuntimeError(haltmsg)
    152         else:
    153             if errmsg:
    154                 asaplog.push(errmsg)
    155                 asaplog.post("WARN")
    156             return False
    157 
    158 
    159109    @asaplog_post_dec
    160110    def plot(self, scan=None):
     
    169119            are consistent e.g. all 'channel' or all 'velocity' etc.
    170120        """
     121        self._startrow = 0
     122        self._ipanel = -1
     123        self._reset_header()
     124        if self._plotter.is_dead:
     125            if self.casabar_exists():
     126                del self._plotter.figmgr.casabar
     127            self._plotter = self._newplotter()
     128            self._plotter.figmgr.casabar=self._new_custombar()
     129        if self.casabar_exists():
     130            self._plotter.figmgr.casabar.set_pagecounter(1)
     131        self._panelrows = []
     132        self._plotter.hold()
     133        #self._plotter.clear()
    171134        if not self._data and not scan:
    172135            msg = "Input is not a scantable"
    173136            raise TypeError(msg)
    174         self._startrow = 0
    175         self._ipanel = -1
    176         self._reset_header()
    177         self._panelrows = []
    178 
    179         self._assert_plotter(action="reload")
    180         if self.casabar_exists():
    181             self._plotter.figmgr.casabar.set_pagecounter(1)
    182 
    183         self._plotter.hold()
    184         #self._plotter.clear()
    185137        if scan:
    186138            self.set_data(scan, refresh=False)
    187         self._plotter.palette(color=0,colormap=self._colormap,
    188                               linestyle=0,linestyles=self._linestyles)
    189         self._plotter.legend(self._legendloc)
    190 
    191139        self._plot(self._data)
    192140        if self._minmaxy is not None:
     
    199147
    200148    def gca(self):
    201         errmsg = "No axis to retun. Need to plot first."
    202         if not self._assert_plotter(action="status",errmsg=errmsg):
    203             return None
    204149        return self._plotter.figure.gca()
    205150
    206151    def refresh(self):
    207152        """Do a soft refresh"""
    208         errmsg = "No figure to re-plot. Need to plot first."
    209         self._assert_plotter(action="halt",errmsg=errmsg)
    210 
    211153        self._plotter.figure.show()
    212154
     
    221163                        if different IFs are spread over panels (default 0)
    222164        """
    223         ## this method relies on already plotted figure
    224         if not self._assert_plotter(action="status") or (self._data is None):
    225             msg = "Cannot create mask interactively on plot. Can only create mask after plotting."
    226             asaplog.push( msg )
    227             asaplog.post( "ERROR" )
     165        if self._data is None:
    228166            return []
    229167        outmask = []
     
    272210    # forwards to matplotlib axes
    273211    def text(self, *args, **kwargs):
    274         self._assert_plotter(action="reload")
    275212        if kwargs.has_key("interactive"):
    276213            if kwargs.pop("interactive"):
     
    282219
    283220    def arrow(self, *args, **kwargs):
    284         self._assert_plotter(action="reload")
    285221        if kwargs.has_key("interactive"):
    286222            if kwargs.pop("interactive"):
     
    295231
    296232    def annotate(self, text, xy=None, xytext=None, **kwargs):
    297         self._assert_plotter(action="reload")
    298233        if kwargs.has_key("interactive"):
    299234            if kwargs.pop("interactive"):
     
    307242
    308243    def axvline(self, *args, **kwargs):
    309         self._assert_plotter(action="reload")
    310244        if kwargs.has_key("interactive"):
    311245            if kwargs.pop("interactive"):
     
    317251
    318252    def axhline(self, *args, **kwargs):
    319         self._assert_plotter(action="reload")
    320253        if kwargs.has_key("interactive"):
    321254            if kwargs.pop("interactive"):
     
    327260
    328261    def axvspan(self, *args, **kwargs):
    329         self._assert_plotter(action="reload")
    330262        if kwargs.has_key("interactive"):
    331263            if kwargs.pop("interactive"):
     
    342274
    343275    def axhspan(self, *args, **kwargs):
    344         self._assert_plotter(action="reload")
    345276        if kwargs.has_key("interactive"):
    346277            if kwargs.pop("interactive"):
     
    357288
    358289    def _axes_callback(self, axesfunc, *args, **kwargs):
    359         self._assert_plotter(action="reload")
    360290        panel = 0
    361291        if kwargs.has_key("panel"):
     
    574504        """
    575505        self._lmap = mp
    576         #self._plotter.legend(mode)
    577         self._legendloc = mode
     506        self._plotter.legend(mode)
    578507        if isinstance(fontsize, int):
    579508            from matplotlib import rc as rcp
     
    584513    def set_title(self, title=None, fontsize=None, refresh=True):
    585514        """
    586         Set the title of sub-plots. If multiple sub-plots are plotted,
     515        Set the title of the plot. If multiple panels are plotted,
    587516        multiple titles have to be specified.
    588517        Parameters:
    589             title:      a list of titles of sub-plots.
    590             fontsize:   a font size of titles (integer)
    591518            refresh:    True (default) or False. If True, the plot is
    592519                        replotted based on the new parameter setting(s).
     
    594521        Example:
    595522             # two panels are visible on the plotter
    596              plotter.set_title(['First Panel','Second Panel'])
     523             plotter.set_title(["First Panel","Second Panel"])
    597524        """
    598525        self._title = title
     
    610537            ordinate:    a list of ordinate labels. None (default) let
    611538                         data determine the labels
    612             fontsize:    a font size of vertical axis labels (integer)
    613539            refresh:     True (default) or False. If True, the plot is
    614540                         replotted based on the new parameter setting(s).
     
    616542        Example:
    617543             # two panels are visible on the plotter
    618              plotter.set_ordinate(['First Y-Axis','Second Y-Axis'])
     544             plotter.set_ordinate(["First Y-Axis","Second Y-Axis"])
    619545        """
    620546        self._ordinate = ordinate
     
    633559            abcissa:     a list of abcissa labels. None (default) let
    634560                         data determine the labels
    635             fontsize:    a font size of horizontal axis labels (integer)
    636561            refresh:     True (default) or False. If True, the plot is
    637562                         replotted based on the new parameter setting(s).
     
    639564        Example:
    640565             # two panels are visible on the plotter
    641              plotter.set_ordinate(['First X-Axis','Second X-Axis'])
     566             plotter.set_ordinate(["First X-Axis","Second X-Axis"])
    642567        """
    643568        self._abcissa = abcissa
     
    659584                        Otherwise,the parameter(s) are set without replotting.
    660585        Example:
    661              plotter.set_colors('red green blue')
     586             plotter.set_colors("red green blue")
    662587             # If for example four lines are overlaid e.g I Q U V
    663588             # 'I' will be 'red', 'Q' will be 'green', U will be 'blue'
    664589             # and 'V' will be 'red' again.
    665590        """
    666         #if isinstance(colmap,str):
    667         #    colmap = colmap.split()
    668         #self._plotter.palette(0, colormap=colmap)
    669         self._colormap = colmap
     591        if isinstance(colmap,str):
     592            colmap = colmap.split()
     593        self._plotter.palette(0, colormap=colmap)
    670594        if refresh and self._data: self.plot(self._data)
    671595
     
    680604                         is taken from the .asaprc setting
    681605                         plotter.histogram
    682             linewidth:   a line width
    683606            refresh:     True (default) or False. If True, the plot is
    684607                         replotted based on the new parameter setting(s).
     
    697620        only one color has been set.
    698621        Parameters:
    699             linestyles:      a list of linestyles to use.
     622             linestyles:     a list of linestyles to use.
    700623                             'line', 'dashed', 'dotted', 'dashdot',
    701624                             'dashdotdot' and 'dashdashdot' are
    702625                             possible
    703             linewidth:       a line width
    704626            refresh:         True (default) or False. If True, the plot is
    705627                             replotted based on the new parameter setting(s).
    706628                             Otherwise,the parameter(s) are set without replotting.
    707629        Example:
    708              plotter.set_colors('black')
    709              plotter.set_linestyles('line dashed dotted dashdot')
     630             plotter.set_colors("black")
     631             plotter.set_linestyles("line dashed dotted dashdot")
    710632             # If for example four lines are overlaid e.g I Q U V
    711633             # 'I' will be 'solid', 'Q' will be 'dashed',
    712634             # U will be 'dotted' and 'V' will be 'dashdot'.
    713635        """
    714         #if isinstance(linestyles,str):
    715         #    linestyles = linestyles.split()
    716         #self._plotter.palette(color=0,linestyle=0,linestyles=linestyles)
    717         self._linestyles = linestyles
     636        if isinstance(linestyles,str):
     637            linestyles = linestyles.split()
     638        self._plotter.palette(color=0,linestyle=0,linestyles=linestyles)
    718639        if isinstance(linewidth, float) or isinstance(linewidth, int):
    719640            from matplotlib import rc as rcp
     
    786707        If the spectrum is flagged no line will be drawn in that location.
    787708        """
    788         errmsg = "Cannot plot spectral lines. Need to plot scantable first."
    789         self._assert_plotter(action="halt",errmsg=errmsg)
    790709        if not self._data:
    791710            raise RuntimeError("No scantable has been plotted yet.")
     
    865784             dpi:         The dpi of the output non-ps plot
    866785        """
    867         errmsg = "Cannot save figure. Need to plot first."
    868         self._assert_plotter(action="halt",errmsg=errmsg)
    869        
    870786        self._plotter.save(filename,orientation,dpi)
    871787        return
     
    875791        """
    876792        Set a plotting mask for a specific polarization.
    877         This is useful for masking out 'noise' Pangle outside a source.
     793        This is useful for masking out "noise" Pangle outside a source.
    878794        Parameters:
    879795             mask:           a mask from scantable.create_mask
     
    884800        Example:
    885801             select = selector()
    886              select.setpolstrings('Pangle')
     802             select.setpolstrings("Pangle")
    887803             plotter.set_mask(mymask, select)
    888804        """
     
    13401256        PL.ion()
    13411257        PL.draw()
    1342         if matplotlib.get_backend() == 'Qt4Agg': PL.gcf().show()
     1258        PL.gcf().show()
    13431259        if (self._outfile is not None):
    13441260           PL.savefig(self._outfile)
     
    13791295        [xmin,xmax,ymin,ymax] = PL.axis()
    13801296        PL.axis([xmax,xmin,ymin,ymax])
    1381         PL.ion()
     1297        #PL.ion()
    13821298        PL.draw()
    1383         if matplotlib.get_backend() == 'Qt4Agg': PL.gcf().show()
     1299        PL.gcf().show()
    13841300        if (self._outfile is not None):
    13851301           PL.savefig(self._outfile)
     
    13891305    @asaplog_post_dec
    13901306    def plottp(self, scan=None, outfile=None):
    1391         self._assert_plotter(action="reload")
     1307        if self._plotter.is_dead:
     1308            if self.casabar_exists():
     1309                del self._plotter.figmgr.casabar
     1310            self._plotter = self._newplotter()
     1311            self._plotter.figmgr.casabar=self._new_custombar()
    13921312        self._plotter.hold()
    13931313        self._plotter.clear()
     
    14791399        See the method help for detailed information.
    14801400        """
    1481         self._assert_plotter(action="reload")
    14821401        self._plotter.text(*args, **kwargs)
    14831402    # end matplotlib.Figure.text forwarding function
     
    15181437
    15191438        if plot:
    1520             errmsg = "Can plot header only after the first call to plot()."
    1521             self._assert_plotter(action="halt",errmsg=errmsg)
    15221439            self._plotter.hold()
    15231440            self._header_plot(headstr,fontsize=fontsize)
     
    15521469            asaplog.push("No header has been plotted. Exit without any operation")
    15531470            asaplog.post("WARN")
    1554         elif self._assert_plotter(action="status"):
     1471        else:
    15551472            self._plotter.hold()
    15561473            for textobj in self._headtext['textobj']:
Note: See TracChangeset for help on using the changeset viewer.