예제 #1
0
파일: marx.py 프로젝트: nplee/ciao-contrib
def plot_marx_spectrum(id=None, elow=None, ehigh=None, ewidth=None,
                       norm=None, overplot=False, clearwindow=True):
    """Plots the spectrum as used by MARX.

    Parameters
    ----------
    id : int or string or None
        If id is None then the default Sherpa id is used. This
        dataset must have a grid and source model defined.
    elow, ehigh, ewidth : number or None
        Only used if all three are given, otherwise the grid
        of the data set is used. The elo value gives the start
        of the grid (the left edge of the first bin) and ehi
        is the end of the grid (the right edge of the last bin),
        and ewidth the bin width, all in keV.
    norm : number or None
        Multiply the flux values by this value, if set.
    overplot : bool, optional
        If ``True`` then the data is added to the current plot,
        otherwise a new plot is created.
    clearwindow: bool, optional
        If ``True`` then clear out the current plot area of all
        existing plots. This is not used if ``overplot`` is set.

    """

    vals = _get_chart_spectrum(id=id, elow=elow,
                               ehigh=ehigh, ewidth=ewidth,
                               norm=norm)

    # In sherpa_contrib.utils.plot_instmap_weights it was found useful
    # to convert the Y values to float32 from float64 to avoid
    # precision issues confusing the plot (i.e. showing structure that
    # isn't meaningful to the user). Is this going to be needed
    # here too?
    #
    hplot = HistogramPlot()
    hplot.xlo = vals['xlo']
    hplot.xhi = vals['xhi']
    hplot.y = vals['y'] / (vals['xhi'] - vals['xlo'])
    hplot.xlabel = 'Energy (keV)'
    hplot.title = 'MARX Spectrum: {}'.format(vals['model'])

    # LaTeX support depends on the backend
    if backend.name == 'pylab':
        hplot.ylabel = 'Flux (photon cm$^{-2}$ s$^{-1}$ keV$^{-1}$)'
    elif backend.name == 'chips':
        hplot.ylabel = 'Flux (photon cm^{-2} s^{-1} keV^{-1})'
    else:
        hplot.ylabel = 'Flux (photon cm^-2 s^-1)'

    hplot.plot(overplot=overplot, clearwindow=clearwindow)
예제 #2
0
파일: utils.py 프로젝트: nplee/ciao-contrib
    def plot(self, overplot=False, clearwindow=True, **kwargs):
        """Plot the weights values.

        Parameters
        ----------
        overplot : bool, optional
            If ``True`` then the data is added to the current plot,
            otherwise a new plot is created.
        clearwindow: bool, optional
            If ``True`` then clear out the current plot area of
            all existing plots. This is not used if ``overplot`` is
            set.
        **kwargs
            Assumed to be plot preferences that override the
            HistogramPlot.histo_plot preferences.

        Notes
        -----
        The data plot preferences are used to control the
        appearance of the plot: at present the following fields
        are used::

            ``xlog``
            ``ylog``
            ``color``

        Examples
        --------

        >>> hplot.plot()

        >>> hplot.plot(ylog=True, color='orange', linestyle='dotted')

        """

        # Create a Sherpa histogram plot. Unlike other plot
        # classes there is no prepare method, which means
        # that we do not have to create a temporary data object,
        # but do have to set the attributes manually.
        #
        # This assumes that it is okay to use a histogram-style
        # plot, since pre CIAO 4.2 there were numeric problems with
        # bin edges that made these plots look ugly, so a "curve"
        # was used.
        #
        # To avoid issues with numeric accuracy (I have seen a
        # case where the variation in the weight values was ~2e-16
        # and matplotlib showed this structure), convert the weights
        # to 32-bit before plotting. This is a lot simpler than
        # dealing with some sort of run-length-encoding scheme to
        # group bins that are "close enough" numerically.
        #
        hplot = HistogramPlot()
        hplot.xlo = self.xlo
        hplot.xhi = self.xhi
        hplot.y = self.weight.astype(np.float32)
        hplot.xlabel = 'Energy (keV)'
        hplot.ylabel = 'Weights'
        hplot.title = 'Weights for {}: '.format(self.id) + \
                      ' {}'.format(self.modelexpr)

        # There is no validation of the preference values.
        #
        # I have removed linestyle from this list since the pylab
        # backend default is 'None', which ends up meaning nothing
        # appears to get drawn. Which is less-than helpful.
        # linecolor also doen't appear to be used, but color is.
        #
        # names = ['xlog', 'ylog', 'linestyle', 'linecolor']
        names = ['xlog', 'ylog', 'color']
        prefs = ui.get_data_plot_prefs()
        for name in names:
            value = prefs.get(name, None)
            if value is not None:
                hplot.histo_prefs[name] = value

        hplot.plot(overplot=overplot, clearwindow=clearwindow, **kwargs)