Пример #1
0
    def plot_on(self,
                ax: plt.axis,
                ylabel="Events",
                draw_legend=True,
                legend_inside=True,
                hide_labels: bool = False):
        bin_edges, bin_mids, bin_width = self._get_bin_edges()

        self._bin_edges = bin_edges
        self._bin_mids = bin_mids
        self._bin_width = bin_width

        ax.hist(
            x=[comp.data for comp in self._mc_components['stacked']],
            bins=bin_edges,
            weights=[comp.weights for comp in self._mc_components['stacked']],
            stacked=True,
            edgecolor="black",
            lw=0.3,
            color=[comp.color for comp in self._mc_components['stacked']],
            label=[comp.label for comp in self._mc_components['stacked']],
            histtype='stepfilled')

        if not hide_labels:
            ax.set_xlabel(self._variable.x_label, plot_style.xlabel_pos)
            y_label = self._get_y_label(False, bin_width, ylabel)
            ax.set_ylabel(y_label, plot_style.ylabel_pos)
        if draw_legend:
            if legend_inside:
                ax.legend(frameon=False)
                ylims = ax.get_ylim()
                ax.set_ylim(ylims[0], 1.4 * ylims[1])

            else:
                ax.legend(frameon=False, bbox_to_anchor=(1, 1))

        return ax
Пример #2
0
    def plot_on(
        self,
        ax1: plt.axis,
        ax2,
        style="stacked",
        ylabel="Events",
        sum_color=plot_style.KITColors.kit_purple,
        draw_legend: bool = True,
        legend_inside: bool = True,
    ):
        bin_edges, bin_mids, bin_width = self._get_bin_edges()

        self._bin_edges = bin_edges
        self._bin_mids = bin_mids
        self._bin_width = bin_width

        sum_w = np.sum(np.array([
            binned_statistic(comp.data,
                             comp.weights,
                             statistic="sum",
                             bins=bin_edges)[0]
            for comp in self._mc_components["MC"]
        ]),
                       axis=0)

        sum_w2 = np.sum(np.array([
            binned_statistic(comp.data,
                             comp.weights**2,
                             statistic="sum",
                             bins=bin_edges)[0]
            for comp in self._mc_components["MC"]
        ]),
                        axis=0)

        hdata, _ = np.histogram(self._data_component.data, bins=bin_edges)

        if style.lower() == "stacked":
            ax1.hist(
                x=[comp.data for comp in self._mc_components['MC']],
                bins=bin_edges,
                weights=[comp.weights for comp in self._mc_components['MC']],
                stacked=True,
                edgecolor="black",
                lw=0.3,
                color=[comp.color for comp in self._mc_components['MC']],
                label=[comp.label for comp in self._mc_components['MC']],
                histtype='stepfilled')

            ax1.bar(x=bin_mids,
                    height=2 * np.sqrt(sum_w2),
                    width=self.bin_width,
                    bottom=sum_w - np.sqrt(sum_w2),
                    color="black",
                    hatch="///////",
                    fill=False,
                    lw=0,
                    label="MC stat. unc.")

        if style.lower() == "summed":
            ax1.bar(x=bin_mids,
                    height=2 * np.sqrt(sum_w2),
                    width=self.bin_width,
                    bottom=sum_w - np.sqrt(sum_w2),
                    color=sum_color,
                    lw=0,
                    label="MC")

        ax1.errorbar(x=bin_mids,
                     y=hdata,
                     yerr=np.sqrt(hdata),
                     ls="",
                     marker=".",
                     color="black",
                     label=self._data_component.label)

        y_label = self._get_y_label(False, bin_width, evts_or_cand=ylabel)
        # ax1.legend(loc=0, bbox_to_anchor=(1,1))
        ax1.set_ylabel(y_label, plot_style.ylabel_pos)

        if draw_legend:
            if legend_inside:
                ax1.legend(frameon=False)
                ylims = ax1.get_ylim()
                ax1.set_ylim(ylims[0], 1.4 * ylims[1])
            else:
                ax1.legend(frameon=False, bbox_to_anchor=(1, 1))

        ax2.set_ylabel(r"$\frac{\mathrm{Data - MC}}{\mathrm{Data}}$")
        ax2.set_xlabel(self._variable.x_label, plot_style.xlabel_pos)
        ax2.set_ylim((-1, 1))

        try:
            uhdata = unp.uarray(hdata, np.sqrt(hdata))
            uhmc = unp.uarray(sum_w, np.sqrt(sum_w2))
            ratio = (uhdata - uhmc) / uhdata

            ax2.axhline(y=0, color=plot_style.KITColors.dark_grey, alpha=0.8)
            ax2.errorbar(bin_mids,
                         unp.nominal_values(ratio),
                         yerr=unp.std_devs(ratio),
                         ls="",
                         marker=".",
                         color=plot_style.KITColors.kit_black)
        except ZeroDivisionError:
            ax2.axhline(y=0, color=plot_style.KITColors.dark_grey, alpha=0.8)

        plt.subplots_adjust(hspace=0.08)
Пример #3
0
    def plot_on(self,
                ax: plt.axis,
                draw_legend: bool = True,
                legend_inside: bool = True,
                yaxis_scale=1.3,
                normed: bool = False,
                ylabel="Events",
                hide_labels: bool = False) -> plt.axis:
        """
        Plots the component on a given matplotlib.pyplot.axis

        :param ax: matplotlib.pyplot.axis where the histograms will be drawn
        on.
        :param draw_legend: Draw legend on axis if True.
        :param normed: If true the histograms are normalized.

        :return: matplotlib.pyplot.axis with histogram drawn on it
        """
        bin_edges, bin_mids, bin_width = self._get_bin_edges()

        self._bin_edges = bin_edges
        self._bin_mids = bin_mids
        self._bin_width = bin_width

        for component in self._mc_components['single']:
            if component.histtype == 'stepfilled':
                alpha = 0.6
                edge_color = 'black'
            else:
                edge_color = None
                alpha = 1.0
            ax.hist(x=component.data,
                    bins=bin_edges,
                    density=normed,
                    weights=component.weights,
                    histtype=component.histtype,
                    label=component.label,
                    edgecolor=edge_color
                    if edge_color is not None else component.color,
                    alpha=alpha,
                    lw=1.5,
                    ls=component.ls,
                    color=component.color)

        if not hide_labels:
            ax.set_xlabel(self._variable.x_label, plot_style.xlabel_pos)

            y_label = self._get_y_label(normed=normed,
                                        bin_width=bin_width,
                                        evts_or_cand=ylabel)
            ax.set_ylabel(y_label, plot_style.ylabel_pos)

        if draw_legend:
            if legend_inside:
                ax.legend(frameon=False)
                ylims = ax.get_ylim()
                ax.set_ylim(ylims[0], yaxis_scale * ylims[1])
            else:
                ax.legend(frameon=False, bbox_to_anchor=(1, 1))

        return ax