def data_mc_ratio(data, mc, label_data='Data', label_mc="MC", y_label=None, figsize=None, ratio_range=(0, 2), *args, **kwarg): """ Perform data mc distributions returns: axes """ f, axes = plt.subplots(2, 1, gridspec_kw={"height_ratios": [3, 1]}, sharex=True, figsize=figsize) ax0 = axes[0] hm = b2plot.hist(mc, lw=2, ax=ax0, label=label_mc, *args, **kwarg) hd = b2plot.errorhist(data, ax=ax0, label=label_data, color='black') ax0.legend() ax1 = axes[1] ry, rye = ratio(hd[0], hm[0]) b2plot.errorbar(hd[1], ry, rye, ax=ax1, color='grey') ax1.axhline(1, color='grey', lw=0.5, ls='--') f.subplots_adjust(hspace=0.1) ax1.set_ylim(*ratio_range) b2plot.xlim() if y_label is not None: ax0.set_ylabel(y_label) ax1.set_ylabel("Ratio") ax1.yaxis.set_label_coords(-0.08, 0.5) ax0.yaxis.set_label_coords(-0.08, 0.5) return axes
def generate(self, cmd=""): if not self.check_file_set(): return if cmd == "": tk.messagebox.showinfo("GUI Python", "There is nothing to build") else: gen = gn.Generator(self.FILENAME) x = gen.x if cmd == "S(0)": y = gen.generate_peaks_without_extension() elif cmd == "S(0)+B": y = gen.generate_bkg_without_stat() + \ gen.generate_peaks_without_extension() elif cmd == "S(1)+B": y = gen.generate_peaks_with_custom_extension() + \ gen.generate_bkg_without_stat() elif cmd == "S(2)+B": if (self.enter_a.get() == "" or self.enter_b.get() == ""): tk.messagebox.showinfo("GUI Python", "Please enter A and B") y = gen.generate_peaks_with_extension(float(self.enter_a.get()), float(self.enter_b.get())) + \ gen.generate_bkg_without_stat() elif cmd == "S(1)+B+D": temp = gen.generate_peaks_with_custom_extension() + \ gen.generate_bkg_without_stat() y = [gn.statistical_scatter(i) for i in temp] elif cmd == "S(2)+B+D": if (self.enter_a.get() == "" or self.enter_b.get() == ""): tk.messagebox.showinfo("GUI Python", "Please enter A and B") temp = gen.generate_peaks_with_extension(float(self.enter_a.get()), float(self.enter_b.get())) + \ gen.generate_bkg_without_stat() y = [gn.statistical_scatter(i) for i in temp] bp.hist(x, weights=y, bins=gen.sts.nbins, style=0) plt.show() return (x, y)
import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import b2plot plt.style.use('belle2') import numpy as np import pandas as pd df = { 'mass': np.append( np.random.random_sample(1000) * 7 - 3.5, np.random.normal(0, 0.5, 1000)) } df = pd.DataFrame(df) b2plot.errorbar(df.sample(1500).mass, color='black', label='Data') b2plot.hist(df.sample(1500).mass, fill=True, lw=2, style=1, label='MC10') plt.legend() b2plot.watermark(fontsize=20, px=.2) b2plot.xlim() b2plot.labels('E', "Events", "GeV", 1) plt.savefig("example_hist_errorbar.pdf")
# -*- coding: utf-8 -*- """ Stacked plot In data science, often one wants to compare two different sets of data, like signal/background or prediction and actual data. In this very brief script we create two sets of data and compare them in one plot. """ import matplotlib.pyplot as plt import pandas as pd import numpy as np import b2plot as bp try: plt.style.use('belle2') except OSError: print("Please install belle2 matplotlib style") bp.hist(np.random.normal(0, 0.5, 1000), label="Pseudo Simulation") bp.errorhist(np.random.normal(0, 0.5, 1000), label="Pseudo Data", color='black') bp.labels("O", "Entries", "Unit") plt.legend() # bp.xlim() # bp.labels('$\Delta M$', "Events", "GeV", 0) bp.save("histogram2.png")
# -*- coding: utf-8 -*- """ Stacked plot In data science, often one wants to compare two different sets of data, like signal/background or prediction and actual data. In this very brief script we create two sets of data and compare them in one plot. """ import matplotlib.pyplot as plt import pandas as pd import numpy as np import b2plot as bp try: plt.style.use('belle2') except OSError: print("Please install belle2 matplotlib style") data = np.random.normal(0, 0.5, 1000) bp.hist(data) bp.labels("O", "Entries", "Unit") # bp.xlim() # bp.labels('$\Delta M$', "Events", "GeV", 0) bp.save("histogram.png")