Example #1
0
        def plot_1D(self, ticket, col, title, xtitle, ytitle, xum="", xrange=None, use_default_factor=True):

            if use_default_factor:
                factor = SRWPlot.get_factor(col)
            else:
                factor = 1.0

            if isinstance(ticket['histogram'].shape, list):
                histogram = ticket['histogram'][0]
            else:
                histogram = ticket['histogram']

            bins = ticket['bins']

            if not xrange is None:
                good = numpy.where((bins >= xrange[0]) & (bins <= xrange[1]))

                bins = bins[good]
                histogram = histogram[good]

            bins *= factor

            self.plot_canvas.addCurve(bins, histogram, title, symbol='', color='blue', replace=True) #'+', '^', ','
            if not xtitle is None: self.plot_canvas.setGraphXLabel(xtitle)
            if not ytitle is None: self.plot_canvas.setGraphYLabel(ytitle)
            if not title is None: self.plot_canvas.setGraphTitle(title)
            self.plot_canvas.setDrawModeEnabled(True, 'rectangle')
            self.plot_canvas.setZoomModeEnabled(True)

            if ticket['fwhm'] == None: ticket['fwhm'] = 0.0

            n_patches = len(self.plot_canvas._backend.ax.patches)
            if (n_patches > 0): self.plot_canvas._backend.ax.patches.remove(self.plot_canvas._backend.ax.patches[n_patches-1])

            if not ticket['fwhm'] == 0.0:
                x_fwhm_i, x_fwhm_f = ticket['fwhm_coordinates']
                x_fwhm_i, x_fwhm_f = x_fwhm_i*factor, x_fwhm_f*factor
                y_fwhm   = max(histogram)*0.5

                self.plot_canvas._backend.ax.add_patch(FancyArrowPatch([x_fwhm_i, y_fwhm],
                                                          [x_fwhm_f, y_fwhm],
                                                          arrowstyle=ArrowStyle.CurveAB(head_width=2, head_length=4),
                                                          color='b',
                                                          linewidth=1.5))
            if min(histogram) < 0:
                self.plot_canvas.setGraphYLimits(min(histogram), max(histogram))
            else:
                self.plot_canvas.setGraphYLimits(0, max(histogram))

            self.plot_canvas.replot()

            self.info_box.total.setText("{:.2e}".format(decimal.Decimal(ticket['total'])))
            self.info_box.fwhm_h.setText("{:5.4f}".format(ticket['fwhm']*factor))
            self.info_box.label_h.setText("FWHM " + xum)
Example #2
0
        def plot_2D(self,
                    ticket,
                    var_x,
                    var_y,
                    title,
                    xtitle,
                    ytitle,
                    xum="",
                    yum="",
                    plotting_range=None,
                    use_default_factor=True):

            matplotlib.rcParams['axes.formatter.useoffset'] = 'False'

            if use_default_factor:
                factor1 = SRWPlot.get_factor(var_x)
                factor2 = SRWPlot.get_factor(var_y)
            else:
                factor1 = 1.0
                factor2 = 1.0

            if plotting_range == None:
                xx = ticket['bin_h']
                yy = ticket['bin_v']

                nbins_h = ticket['nbins_h']
                nbins_v = ticket['nbins_v']

                histogram = ticket['histogram']
            else:
                range_x = numpy.where(
                    numpy.logical_and(ticket['bin_h'] >= plotting_range[0],
                                      ticket['bin_h'] <= plotting_range[1]))
                range_y = numpy.where(
                    numpy.logical_and(ticket['bin_v'] >= plotting_range[2],
                                      ticket['bin_v'] <= plotting_range[3]))

                xx = ticket['bin_h'][range_x]
                yy = ticket['bin_v'][range_y]

                histogram = []
                for row in ticket['histogram'][range_x]:
                    histogram.append(row[range_y])

                nbins_h = len(xx)
                nbins_v = len(yy)

            if len(xx) == 0 or len(yy) == 0:
                raise Exception("Nothing to plot in the given range")

            xmin, xmax = xx.min(), xx.max()
            ymin, ymax = yy.min(), yy.max()

            origin = (xmin * factor1, ymin * factor2)
            scale = (abs(
                (xmax - xmin) / nbins_h) * factor1, abs(
                    (ymax - ymin) / nbins_v) * factor2)

            # PyMCA inverts axis!!!! histogram must be calculated reversed
            data_to_plot = []
            for y_index in range(0, nbins_v):
                x_values = []
                for x_index in range(0, nbins_h):
                    x_values.append(histogram[x_index][y_index])

                data_to_plot.append(x_values)

            self.plot_canvas.setImage(numpy.array(data_to_plot),
                                      origin=origin,
                                      scale=scale)

            if xtitle is None: xtitle = SRWPlot.get_SRW_label(var_x)
            if ytitle is None: ytitle = SRWPlot.get_SRW_label(var_y)

            self.plot_canvas.setGraphXLabel(xtitle)
            self.plot_canvas.setGraphYLabel(ytitle)
            self.plot_canvas.setGraphTitle(title)

            self.plot_canvas._histoHPlot.setGraphYLabel('Frequency')

            self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
            ).set_color('white')
            self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
            ).set_fontsize(1)
            for label in self.plot_canvas._histoHPlot._backend.ax.xaxis.get_ticklabels(
            ):
                label.set_color('white')
                label.set_fontsize(1)

            self.plot_canvas._histoVPlot.setGraphXLabel('Frequency')

            self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
            ).set_color('white')
            self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
            ).set_fontsize(1)
            for label in self.plot_canvas._histoVPlot._backend.ax.yaxis.get_ticklabels(
            ):
                label.set_color('white')
                label.set_fontsize(1)

            if ticket['fwhm_h'] == None: ticket['fwhm_h'] = 0.0
            if ticket['fwhm_v'] == None: ticket['fwhm_v'] = 0.0

            n_patches = len(self.plot_canvas._histoHPlot._backend.ax.patches)
            if (n_patches > 0):
                self.plot_canvas._histoHPlot._backend.ax.patches.remove(
                    self.plot_canvas._histoHPlot._backend.ax.patches[n_patches
                                                                     - 1])

            if not ticket['fwhm_h'] == 0.0:
                x_fwhm_i, x_fwhm_f = ticket['fwhm_coordinates_h']
                x_fwhm_i, x_fwhm_f = x_fwhm_i * factor1, x_fwhm_f * factor1
                y_fwhm = max(ticket['histogram_h']) * 0.5

                self.plot_canvas._histoHPlot._backend.ax.add_patch(
                    FancyArrowPatch([x_fwhm_i, y_fwhm], [x_fwhm_f, y_fwhm],
                                    arrowstyle=ArrowStyle.CurveAB(
                                        head_width=2, head_length=4),
                                    color='b',
                                    linewidth=1.5))

            n_patches = len(self.plot_canvas._histoVPlot._backend.ax.patches)
            if (n_patches > 0):
                self.plot_canvas._histoVPlot._backend.ax.patches.remove(
                    self.plot_canvas._histoVPlot._backend.ax.patches[n_patches
                                                                     - 1])

            if not ticket['fwhm_v'] == 0.0:
                y_fwhm_i, y_fwhm_f = ticket['fwhm_coordinates_v']
                y_fwhm_i, y_fwhm_f = y_fwhm_i * factor2, y_fwhm_f * factor2
                x_fwhm = max(ticket['histogram_v']) * 0.5

                self.plot_canvas._histoVPlot._backend.ax.add_patch(
                    FancyArrowPatch([x_fwhm, y_fwhm_i], [x_fwhm, y_fwhm_f],
                                    arrowstyle=ArrowStyle.CurveAB(
                                        head_width=2, head_length=4),
                                    color='r',
                                    linewidth=1.5))

            self.plot_canvas._histoHPlot.replot()
            self.plot_canvas._histoVPlot.replot()
            self.plot_canvas.replot()

            self.info_box.total.setText("{:.3e}".format(
                decimal.Decimal(ticket['total'])))
            self.info_box.fwhm_h.setText("{:5.4f}".format(ticket['fwhm_h'] *
                                                          factor1))
            self.info_box.fwhm_v.setText("{:5.4f}".format(ticket['fwhm_v'] *
                                                          factor2))
            self.info_box.label_h.setText("FWHM " + xum)
            self.info_box.label_v.setText("FWHM " + yum)
Example #3
0
    def plot_2D(self,
                histogram,
                xx=None,
                yy=None,
                title="",
                xtitle="",
                ytitle="",
                xum="[mm]",
                yum="[mm]",
                plotting_range=None,
                factor1=1.0,
                factor2=1.0,
                colormap=None):

        if xx is None:
            xx = numpy.arange(histogram.shape[0])

        if yy is None:
            yy = numpy.arange(histogram.shape[1])

        if plotting_range == None:
            nbins_h = xx.size
            nbins_v = yy.size
        else:
            range_x = numpy.where(
                numpy.logical_and(xx >= plotting_range[0],
                                  xx <= plotting_range[1]))
            range_y = numpy.where(
                numpy.logical_and(yy >= plotting_range[2],
                                  yy <= plotting_range[3]))

            xx = xx[range_x]
            yy = yy[range_y]

            nbins_h = xx.size
            nbins_v = yy.size

        if len(xx) == 0 or len(yy) == 0:
            raise Exception("Nothing to plot in the given range")

        xmin, xmax = xx.min(), xx.max()
        ymin, ymax = yy.min(), yy.max()

        origin = (xmin * factor1, ymin * factor2)
        scale = (abs(
            (xmax - xmin) / nbins_h) * factor1, abs(
                (ymax - ymin) / nbins_v) * factor2)

        # silx inverts axis!!!! histogram must be calculated reversed
        data_to_plot = []
        for y_index in range(0, nbins_v):
            x_values = []
            for x_index in range(0, nbins_h):
                x_values.append(histogram[x_index][y_index])

            data_to_plot.append(x_values)

        data_to_plot = numpy.array(data_to_plot)

        histogram_h = numpy.sum(data_to_plot,
                                axis=0)  # data to plot axis are inverted
        histogram_v = numpy.sum(data_to_plot, axis=1)

        ticket = {}
        ticket['total'] = numpy.sum(data_to_plot)

        ticket['fwhm_h'], ticket['fwhm_quote_h'], ticket[
            'fwhm_coordinates_h'] = get_fwhm(histogram_h, xx)
        ticket['sigma_h'] = get_sigma(histogram_h, xx)

        ticket['fwhm_v'], ticket['fwhm_quote_v'], ticket[
            'fwhm_coordinates_v'] = get_fwhm(histogram_v, yy)
        ticket['sigma_v'] = get_sigma(histogram_v, yy)

        self.plot_canvas.setColormap(colormap=colormap)
        self.plot_canvas.setImage(data_to_plot, origin=origin, scale=scale)

        self.plot_canvas.setGraphXLabel(xtitle)
        self.plot_canvas.setGraphYLabel(ytitle)
        self.plot_canvas.setGraphTitle(title)

        self.plot_canvas._histoHPlot.setGraphYLabel('Counts')

        self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label().set_color(
            'white')
        self.plot_canvas._histoHPlot._backend.ax.xaxis.get_label(
        ).set_fontsize(1)
        for label in self.plot_canvas._histoHPlot._backend.ax.xaxis.get_ticklabels(
        ):
            label.set_color('white')
            label.set_fontsize(1)

        self.plot_canvas._histoVPlot.setGraphXLabel('Counts')

        self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label().set_color(
            'white')
        self.plot_canvas._histoVPlot._backend.ax.yaxis.get_label(
        ).set_fontsize(1)
        for label in self.plot_canvas._histoVPlot._backend.ax.yaxis.get_ticklabels(
        ):
            label.set_color('white')
            label.set_fontsize(1)

        n_patches = len(self.plot_canvas._histoHPlot._backend.ax.patches)
        if (n_patches > 0):
            self.plot_canvas._histoHPlot._backend.ax.patches.remove(
                self.plot_canvas._histoHPlot._backend.ax.patches[n_patches -
                                                                 1])

        if not ticket['fwhm_h'] == 0.0:
            x_fwhm_i, x_fwhm_f = ticket['fwhm_coordinates_h']
            x_fwhm_i, x_fwhm_f = x_fwhm_i * factor1, x_fwhm_f * factor1
            y_fwhm = ticket['fwhm_quote_h']

            self.plot_canvas._histoHPlot._backend.ax.add_patch(
                FancyArrowPatch([x_fwhm_i, y_fwhm], [x_fwhm_f, y_fwhm],
                                arrowstyle=ArrowStyle.CurveAB(head_width=2,
                                                              head_length=4),
                                color='b',
                                linewidth=1.5))

        n_patches = len(self.plot_canvas._histoVPlot._backend.ax.patches)
        if (n_patches > 0):
            self.plot_canvas._histoVPlot._backend.ax.patches.remove(
                self.plot_canvas._histoVPlot._backend.ax.patches[n_patches -
                                                                 1])

        if not ticket['fwhm_v'] == 0.0:
            y_fwhm_i, y_fwhm_f = ticket['fwhm_coordinates_v']
            y_fwhm_i, y_fwhm_f = y_fwhm_i * factor2, y_fwhm_f * factor2
            x_fwhm = ticket['fwhm_quote_v']

            self.plot_canvas._histoVPlot._backend.ax.add_patch(
                FancyArrowPatch([x_fwhm, y_fwhm_i], [x_fwhm, y_fwhm_f],
                                arrowstyle=ArrowStyle.CurveAB(head_width=2,
                                                              head_length=4),
                                color='r',
                                linewidth=1.5))

        self.plot_canvas._histoHPlot.replot()
        self.plot_canvas._histoVPlot.replot()
        self.plot_canvas.replot()

        self.info_box.total.setText("{:.3e}".format(
            decimal.Decimal(ticket['total'])))
        self.info_box.fwhm_h.setText("{:5.4f}".format(ticket['fwhm_h'] *
                                                      factor1))
        self.info_box.fwhm_v.setText("{:5.4f}".format(ticket['fwhm_v'] *
                                                      factor2))
        self.info_box.label_h.setText("FWHM " + xum)
        self.info_box.label_v.setText("FWHM " + yum)
        self.info_box.sigma_h.setText("{:5.4f}".format(ticket['sigma_h'] *
                                                       factor1))
        self.info_box.sigma_v.setText("{:5.4f}".format(ticket['sigma_v'] *
                                                       factor2))
        self.info_box.label_s_h.setText("\u03c3 " + xum)
        self.info_box.label_s_v.setText("\u03c3 " + yum)