예제 #1
0
 def plot_data_vs_prediction(self, key_meas_, key_pred_, **kwargs):
     x_m, y_m, [ey_lo_m, ey_hi_m
                ], ex_m = HEPData_plt.get_1D_distribution(self, key_meas_)
     x_p, y_p, [ey_lo_p, ey_hi_p
                ], ex_p = HEPData_plt.get_1D_distribution(self, key_pred_)
     fig = plt.figure(figsize=(5, 7))
     ax1 = fig.add_subplot(211)
     ax1.errorbar(x_p,
                  y_p,
                  yerr=[ey_lo_p, ey_hi_p],
                  xerr=ex_p,
                  c='r',
                  linestyle='None',
                  marker='+',
                  alpha=0.8,
                  label="Prediction")
     ax1.errorbar(x_m,
                  y_m,
                  yerr=[ey_lo_m, ey_hi_m],
                  xerr=ex_m,
                  c='k',
                  linestyle='None',
                  alpha=1,
                  label="Data")
     ax1.legend(loc=kwargs.get("legend_loc", "best"))
     plt.ylabel(kwargs.get("ylabel", "observable"))
     plt.title(kwargs.get("title", ""))
     if "xlim" in kwargs:
         ax1.axis(xmin=kwargs["xlim"][0], xmax=kwargs["xlim"][1])
     if "ylim" in kwargs:
         ax1.axis(ymin=kwargs["ylim"][0], ymax=kwargs["ylim"][1])
     plt.grid()
     ax2 = fig.add_subplot(212)
     ax2.errorbar(x_p,
                  y_p / y_p,
                  yerr=[ey_lo_p / y_p, ey_hi_p / y_p],
                  xerr=ex_p,
                  c='r',
                  linestyle='None',
                  marker='+',
                  alpha=0.8)
     ax2.errorbar(x_m,
                  y_m / y_p,
                  yerr=[ey_lo_m / y_p, ey_hi_m / y_p],
                  xerr=ex_m,
                  c='k',
                  linestyle='None',
                  alpha=1)
     if "xlim" in kwargs:
         ax2.axis(xmin=kwargs["xlim"][0], xmax=kwargs["xlim"][1])
     plt.ylabel("Measured / prediction")
     if "xlabel" in kwargs: plt.xlabel(kwargs["xlabel"])
     plt.grid()
     plt.show()
예제 #2
0
 def plot_1D_distribution(self, key_, **kwargs):
     x, y, [ey_lo, ey_hi], ex = HEPData_plt.get_1D_distribution(self, key_)
     fig = plt.figure(figsize=(5, 5))
     ax = fig.add_subplot(111)
     ax.errorbar(x,
                 y,
                 yerr=[ey_lo, ey_hi],
                 xerr=ex,
                 c='k',
                 linestyle='None',
                 marker='+',
                 alpha=0.6,
                 label=kwargs.get("label", "label"))
     ax.legend(loc=kwargs.get("legend_loc", "best"))
     plt.xlabel(kwargs.get("xlabel", "observable"))
     plt.ylabel(kwargs.get("ylabel", "observable"))
     plt.title(kwargs.get("title", ""))
     if "xlim" in kwargs:
         ax.axis(xmin=kwargs["xlim"][0], xmax=kwargs["xlim"][1])
     if "ylim" in kwargs:
         ax.axis(ymin=kwargs["ylim"][0], ymax=kwargs["ylim"][1])
     plt.grid()
     plt.show()
예제 #3
0
 def plot_1D_distribution(self, key_, **kwargs):
     if self._distributions_1D[key_]._has_errors:
         msg.error(
             "Distribution_store.plot_1D_distribution",
             "Key {0} had some errors when loading. Please clear them before plotting."
             .format(key_), -1)
         return
     x, y, [ey_lo,
            ey_hi], ex, labels, keys = HEPData_plt.get_1D_distribution(
                self, key_)
     x, y, [ey_lo_sys, ey_hi_sys
            ], ex, labels, sys_keys = HEPData_plt.get_1D_distribution(
                self, key_, "sys")
     x, y, [ey_lo_stat, ey_hi_stat
            ], ex, labels, stat_keys = HEPData_plt.get_1D_distribution(
                self, key_, "stat")
     fig = plt.figure(figsize=(15, 5))
     ax = fig.add_subplot(111)
     str_tot_legend = kwargs.get(
         "label", "distribution") + " ( " + " + ".join(keys) + " )"
     str_tot_legend = "\n".join([
         str_tot_legend[120 * i:min(len(str_tot_legend), 120 * (i + 1))]
         for i in range(int(len(str_tot_legend) / 120) + 1)
     ])
     str_sys_legend = kwargs.get(
         "label", "distribution") + " ( " + " + ".join(sys_keys) + " )"
     str_sys_legend = "\n".join([
         str_sys_legend[120 * i:min(len(str_sys_legend), 120 * (i + 1))]
         for i in range(int(len(str_sys_legend) / 120) + 1)
     ])
     if sum([np.fabs(x) for x in ey_hi_sys + ey_lo_sys]) > 0:
         ax.errorbar(x,
                     y,
                     yerr=[ey_lo_sys, ey_hi_sys],
                     c='royalblue',
                     linewidth=18,
                     linestyle='None',
                     marker='None',
                     alpha=0.4,
                     label=str_tot_legend)
     if sum([np.fabs(x) for x in ey_hi_stat + ey_lo_stat]) > 0:
         ax.errorbar(x,
                     y,
                     yerr=[ey_lo_stat, ey_hi_stat],
                     c='indianred',
                     linewidth=6,
                     linestyle='None',
                     marker='None',
                     alpha=0.6,
                     label=kwargs.get("label", "distribution") +
                     " ( stat )")
     ax.errorbar(x,
                 y,
                 yerr=[ey_lo, ey_hi],
                 xerr=ex,
                 c='k',
                 linewidth=2,
                 linestyle='None',
                 marker='+',
                 alpha=1,
                 label=str_sys_legend)
     if labels:
         ax.set_xticks(x)
         ax.set_xticklabels(self._distributions_1D[key_]._bin_labels,
                            rotation=45)
     box = ax.get_position()
     ax.set_position([box.x0, box.y0, box.width * 0.4, box.height])
     if "legend_loc" in kwargs:
         ax.legend(loc=kwargs.get("legend_loc", "best"))
     else:
         ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
     plt.xlabel(
         kwargs.get("xlabel", self._distributions_1D[key_]._indep_var))
     plt.ylabel(kwargs.get("ylabel", self._distributions_1D[key_]._dep_var))
     plt.title(kwargs.get("title", ""))
     ax.axis(xlim=kwargs.get("xlim", [x[0], x[len(x) - 1]]))
     ax.axis(ylim=kwargs.get("ylim", [x[0], x[len(x) - 1]]))
     if kwargs.get("logy", False) is True: plt.yscale("log")
     if kwargs.get("logx", False) is True: plt.xscale("log")
     plt.grid()
     plt.show()
예제 #4
0
 def plot_data_vs_prediction(self, key_meas_, key_pred_, **kwargs):
     x_m, y_m, [
         ey_lo_m, ey_hi_m
     ], ex_m, labels, keys_meas = HEPData_plt.get_1D_distribution(
         self, key_meas_)
     x_p, y_p, [
         ey_lo_p, ey_hi_p
     ], ex_p, labels, keys_pred = HEPData_plt.get_1D_distribution(
         self, key_pred_)
     fig = plt.figure(figsize=(15, 7))
     ax1 = fig.add_subplot(211)
     ax1.errorbar(x_p,
                  y_p,
                  yerr=[ey_lo_p, ey_hi_p],
                  xerr=ex_p,
                  c='r',
                  linestyle='None',
                  marker='+',
                  alpha=0.8,
                  label="Prediction ( " + " $\oplus$ ".join(keys_meas) +
                  " )")
     ax1.errorbar(x_m,
                  y_m,
                  yerr=[ey_lo_m, ey_hi_m],
                  xerr=ex_m,
                  c='k',
                  linestyle='None',
                  alpha=1,
                  label="Data ( " + " $\oplus$ ".join(keys_pred) + " )")
     box = ax1.get_position()
     ax1.set_position([box.x0, box.y0, box.width * 0.4, box.height])
     if "legend_loc" in kwargs:
         ax1.legend(loc=kwargs.get("legend_loc", "best"))
     else:
         ax1.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
     plt.ylabel(kwargs.get("ylabel", "observable"))
     plt.title(kwargs.get("title", ""))
     ax.axis(xlim=kwargs.get("xlim", [x_m[0], x_m[len(x_m) - 1]]))
     ax.axis(ylim=kwargs.get("ylim", [x_m[0], x_m[len(x_m) - 1]]))
     if kwargs.get("logy", False) is True: plt.yscale("log")
     if kwargs.get("logx", False) is True: plt.xscale("log")
     plt.grid()
     ax2 = fig.add_subplot(212)
     ax2.errorbar(x_p,
                  y_p / y_p,
                  yerr=[ey_lo_p / y_p, ey_hi_p / y_p],
                  xerr=ex_p,
                  c='r',
                  linestyle='None',
                  marker='+',
                  alpha=0.8)
     ax2.errorbar(x_m,
                  y_m / y_p,
                  yerr=[ey_lo_m / y_p, ey_hi_m / y_p],
                  xerr=ex_m,
                  c='k',
                  linestyle='None',
                  alpha=1)
     box = ax2.get_position()
     ax2.set_position([box.x0, box.y0, box.width * 0.4, box.height])
     ax.axis(xlim=kwargs.get("xlim", [x_m[0], x_m[len(x_m) - 1]]))
     plt.ylabel("Measured / prediction")
     if "xlabel" in kwargs: plt.xlabel(kwargs["xlabel"])
     if labels:
         ax2.set_xticks(x)
         ax2.set_xticklabels(self._distributions_1D[key_]._bin_labels,
                             rotation=45)
     plt.grid()
     plt.show()
def plot_ratio_1D(table_num_, table_den_, **kwargs):
    x_n, y_n, [ex_lo_n, ex_hi_n], [
        ey_lo_n, ey_hi_n
    ], labels, keys_num = plotter.get_1D_distribution(table_num_)
    x_d, y_d, [ex_lo_d, ex_hi_d], [
        ey_lo_d, ey_hi_d
    ], labels, keys_den = plotter.get_1D_distribution(table_den_)
    chi2 = hlp.get_chi2(y_n, ey_lo_n, ey_hi_n, y_d, ey_lo_d, ey_hi_d)
    for i in range(len(x_n)):
        if x_n[i] == x_d[i]: continue
        msg.error("plot_ratio_1D", "Arguments do not have the same binning")
        raise ValueError(
            "Ratio of distributions with bin centres at {0} and {1}", x_n, x_d)
    fig = plotter.plt.figure(figsize=(10, 10))
    ax1 = fig.add_subplot(211)
    legend_char_width = 53
    str_num_legend = "TABLE 1 ( " + " + ".join(keys_num) + " )"
    str_num_legend = "\n".join([
        str_num_legend[legend_char_width *
                       i:min(len(str_num_legend), legend_char_width * (i + 1))]
        for i in range(int(len(str_num_legend) / legend_char_width) + 1)
    ])
    str_den_legend = "TABLE 2 ( " + " + ".join(keys_den) + " )"
    str_den_legend = "\n".join([
        str_den_legend[legend_char_width *
                       i:min(len(str_den_legend), legend_char_width * (i + 1))]
        for i in range(int(len(str_den_legend) / legend_char_width) + 1)
    ])
    ax1.errorbar(x_n,
                 y_n,
                 yerr=[ey_lo_n, ey_hi_n],
                 xerr=[ex_lo_n, ex_hi_n],
                 c='k',
                 linewidth=2,
                 linestyle='None',
                 alpha=0.8,
                 label=str_num_legend)
    ax1.errorbar(x_d,
                 y_d,
                 yerr=[ey_lo_d, ey_hi_d],
                 xerr=[ex_lo_d, ex_hi_d],
                 c='r',
                 linewidth=4,
                 linestyle='None',
                 alpha=0.4,
                 label=str_den_legend)
    ax1.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plotter.plt.subplots_adjust(left=0.1, right=0.5, top=0.95, bottom=0.4)
    plotter.plt.ylabel("Values")
    plotter.plt.ylabel("Values")
    xlim = kwargs.get(
        "xlim", [x_d[0] - np.fabs(ex_lo_n[0]), x_d[-1] + np.fabs(ex_hi_n[-1])])
    ymin, ymax = ax1.get_ylim()
    ax1.axis(xmin=xlim[0], xmax=xlim[1])
    try:
        plotter.plt.text(
            xlim[0], 1.19 * ymax - 0.19 * ymin,
            ("$\\bf{TABLE}$ $\\bf{1:}$  " + table_num_._dep_var._name).replace(
                "\\\\", "\\").replace(r"\text{", r"{\rm "))
        plotter.plt.text(
            xlim[0], 1.08 * ymax - 0.08 * ymin,
            ("$\\bf{TABLE}$ $\\bf{2:}$  " + table_den_._dep_var._name).replace(
                "\\\\", "\\").replace(r"\text{", r"{\rm "))
    except:
        msg.warning(
            "plot_ratio_1D",
            "could not render observable name - no title given to plot")
    plotter.plt.grid()
    ax2 = fig.add_subplot(212)
    ax2.errorbar(x_n,
                 y_n / y_d,
                 yerr=[ey_lo_n / y_d, ey_hi_n / y_d],
                 xerr=[ex_lo_n, ex_hi_n],
                 c='k',
                 linewidth=2,
                 linestyle='None',
                 alpha=0.8)
    ax2.errorbar(x_d,
                 y_d / y_d,
                 yerr=[ey_lo_d / y_d, ey_hi_d / y_d],
                 xerr=[ex_lo_d, ex_hi_d],
                 c='r',
                 linewidth=4,
                 linestyle='None',
                 alpha=0.4)
    box = ax2.get_position()
    ax2.set_position([box.x0, box.y0, box.width * 0.4, box.height])
    ax2.axis(xmin=xlim[0], xmax=xlim[1])
    plotter.plt.ylabel("Ratio  $\\bf{vs.}$  TABLE 2")
    try:
        plotter.plt.xlabel(
            kwargs.get(
                "xlabel", table_den_._indep_vars[0].name().replace(
                    "\\\\", "\\").replace(r"\text{", r"{\rm ")))
    except:
        plotter.plt.xlabel("<error reading xlabel")
    plotter.plt.subplots_adjust(left=0.1, right=0.5, top=0.92, bottom=0.4)
    plotter.plt.grid()
    if kwargs.get("show", False):
        plotter.plt.show()
    if kwargs.get("save", False):
        fig.savefig(plotter.document, format='pdf')
    plotter.plt.close(fig)
    return chi2, y_n / y_d