def test_plotratio(): # histogram creation and manipulation from coffea import hist # matplotlib import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt lepton_kinematics = fill_lepton_kinematics() # Add some pseudodata to a pt histogram so we can make a nice data/mc plot pthist = lepton_kinematics.sum('eta') bin_values = pthist.axis('pt').centers() poisson_means = pthist.sum('flavor').values()[()] values = np.repeat(bin_values, np.random.poisson(poisson_means)) pthist.fill(flavor='pseudodata', pt=values) # Set nicer labels, by accessing the string bins' label property pthist.axis('flavor').index('electron').label = 'e Flavor' pthist.axis('flavor').index('muon').label = r'$\mu$ Flavor' pthist.axis('flavor').index( 'pseudodata').label = r'Pseudodata from e/$\mu$' # using regular expressions on flavor name to select just the data # another method would be to fill a separate data histogram import re notdata = re.compile('(?!pseudodata)') # make a nice ratio plot plt.rcParams.update({ 'font.size': 14, 'axes.titlesize': 18, 'axes.labelsize': 18, 'xtick.labelsize': 12, 'ytick.labelsize': 12 }) fig, (ax, rax) = plt.subplots(2, 1, figsize=(7, 7), gridspec_kw={"height_ratios": (3, 1)}, sharex=True) fig.subplots_adjust(hspace=.07) # Here is an example of setting up a color cycler to color the various fill patches # http://colorbrewer2.org/#type=qualitative&scheme=Paired&n=6 from cycler import cycler colors = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c'] ax.set_prop_cycle(cycler(color=colors)) fill_opts = {'edgecolor': (0, 0, 0, 0.3), 'alpha': 0.8} error_opts = { 'label': 'Stat. Unc.', 'hatch': '///', 'facecolor': 'none', 'edgecolor': (0, 0, 0, .5), 'linewidth': 0 } data_err_opts = { 'linestyle': 'none', 'marker': '.', 'markersize': 10., 'color': 'k', 'elinewidth': 1, 'emarker': '_' } hist.plot1d(pthist[notdata], overlay="flavor", ax=ax, clear=False, stack=True, line_opts=None, fill_opts=fill_opts, error_opts=error_opts) hist.plot1d(pthist['pseudodata'], overlay="flavor", ax=ax, clear=False, error_opts=data_err_opts) ax.autoscale(axis='x', tight=True) ax.set_ylim(0, None) ax.set_xlabel(None) leg = ax.legend() hist.plotratio(pthist['pseudodata'].sum("flavor"), pthist[notdata].sum("flavor"), ax=rax, error_opts=data_err_opts, denom_fill_opts={}, guide_opts={}, unc='num') rax.set_ylabel('Ratio') rax.set_ylim(0, 2) coffee = plt.text(0., 1., u"☕", fontsize=28, horizontalalignment='left', verticalalignment='bottom', transform=ax.transAxes) lumi = plt.text(1., 1., r"1 fb$^{-1}$ (?? TeV)", fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes)
def test_plotratio(): # histogram creation and manipulation from coffea import hist # matplotlib import matplotlib.pyplot as plt plt.switch_backend("agg") lepton_kinematics = fill_lepton_kinematics() # Add some pseudodata to a pt histogram so we can make a nice data/mc plot pthist = lepton_kinematics.sum("eta") bin_values = pthist.axis("pt").centers() poisson_means = pthist.sum("flavor").values()[()] values = np.repeat(bin_values, np.random.poisson(poisson_means)) pthist.fill(flavor="pseudodata", pt=values) # Set nicer labels, by accessing the string bins' label property pthist.axis("flavor").index("electron").label = "e Flavor" pthist.axis("flavor").index("muon").label = r"$\mu$ Flavor" pthist.axis("flavor").index( "pseudodata").label = r"Pseudodata from e/$\mu$" # using regular expressions on flavor name to select just the data # another method would be to fill a separate data histogram import re notdata = re.compile("(?!pseudodata)") # make a nice ratio plot plt.rcParams.update({ "font.size": 14, "axes.titlesize": 18, "axes.labelsize": 18, "xtick.labelsize": 12, "ytick.labelsize": 12, }) fig, (ax, rax) = plt.subplots(2, 1, figsize=(7, 7), gridspec_kw={"height_ratios": (3, 1)}, sharex=True) fig.subplots_adjust(hspace=0.07) # Here is an example of setting up a color cycler to color the various fill patches # http://colorbrewer2.org/#type=qualitative&scheme=Paired&n=6 from cycler import cycler colors = ["#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", "#e31a1c"] ax.set_prop_cycle(cycler(color=colors)) fill_opts = {"edgecolor": (0, 0, 0, 0.3), "alpha": 0.8} error_opts = { "label": "Stat. Unc.", "hatch": "///", "facecolor": "none", "edgecolor": (0, 0, 0, 0.5), "linewidth": 0, } data_err_opts = { "linestyle": "none", "marker": ".", "markersize": 10.0, "color": "k", "elinewidth": 1, } hist.plot1d( pthist[notdata], overlay="flavor", ax=ax, clear=False, stack=True, line_opts=None, fill_opts=fill_opts, error_opts=error_opts, ) hist.plot1d( pthist["pseudodata"], overlay="flavor", ax=ax, clear=False, error_opts=data_err_opts, ) ax.autoscale(axis="x", tight=True) ax.set_ylim(0, None) ax.set_xlabel(None) ax.legend() hist.plotratio( pthist["pseudodata"].sum("flavor"), pthist[notdata].sum("flavor"), ax=rax, error_opts=data_err_opts, denom_fill_opts={}, guide_opts={}, unc="num", ) rax.set_ylabel("Ratio") rax.set_ylim(0, 2) plt.text( 0.0, 1.0, "☕", fontsize=28, horizontalalignment="left", verticalalignment="bottom", transform=ax.transAxes, ) plt.text( 1.0, 1.0, r"1 fb$^{-1}$ (?? TeV)", fontsize=16, horizontalalignment="right", verticalalignment="bottom", transform=ax.transAxes, )