import matplotlib.pyplot as plt
import numpy as np
import uproot
import heputils
heputils.plot.set_style("ATLAS")
heputils.plot.set_experiment_info(status="Internal") # Internal, Preliminary, Public
heputils.plot.set_experiment_info(
center_of_mass_energy=13, center_of_mass_energy_units="TeV"
) # Default values
heputils.plot.set_experiment_info(
luminosity=132, luminosity_units="fb"
) # Default values
root_file = uproot.open("example.root")
root_hists = [root_file[key].to_hist() for key in root_file.keys()]
data_hist = root_hists[-1]
simulation_hists = root_hists[:-1]
labels = [r"$t\bar{t}$", r"$W+$jets", "Other", "Signal"]
colormap = ["darkorange", "steelblue", "firebrick", "purple"]
scale_factors = np.ones(len(simulation_hists))
scale_factors[-1] = 5
fig, ax = plt.subplots()
ax = heputils.plot.stack_hist(
simulation_hists,
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
scale_factors=scale_factors,
ax=ax,
)
ax = heputils.plot.data_hist(data_hist, ax=ax);
fig.savefig("example_stack.png")
Can also give the data histogram directly to heputils.plot.stack_hist
ax, artists = heputils.plot.stack_hist(
simulation_hists,
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
data_hist=data_hist,
logy=False,
return_artists=True,
)
Can easily plot on a subplots
grid
_fig_size = heputils.plot.get_style()["figure.figsize"]
fig, axs = plt.subplots(1, 2, figsize=(2.1 * _fig_size[0], _fig_size[1]))
axs[0] = heputils.plot.stack_hist(
simulation_hists,
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
data_hist=data_hist,
ax=axs[0],
)
axs[1] = heputils.plot.stack_hist(
simulation_hists,
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
data_hist=data_hist,
logy=False,
ax=axs[1],
)
_fig_size = heputils.plot.get_style()["figure.figsize"]
fig, axs = plt.subplots(1, 2, figsize=(2.1 * _fig_size[0], _fig_size[1]))
axs[0] = heputils.plot.shape_hist(
simulation_hists,
data_hist=data_hist,
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
ax=axs[0],
)
axs[1] = heputils.plot.shape_hist(
simulation_hists,
data_hist=data_hist,
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
logy=True,
density=False,
ax=axs[1],
)
For a shape plot you can also easily switch between a fill
style (shaded) and a step
style (lines) using the histtype
kwarg
_fig_size = heputils.plot.get_style()["figure.figsize"]
fig, axs = plt.subplots(1, 2, figsize=(2.1 * _fig_size[0], _fig_size[1]))
axs[0] = heputils.plot.shape_hist(
simulation_hists,
data_hist=data_hist,
labels=labels,
color=colormap,
histtype="step",
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
ax=axs[0],
)
axs[1] = heputils.plot.shape_hist(
simulation_hists,
data_hist=data_hist,
labels=labels,
color=colormap,
histtype="step",
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
logy=True,
density=False,
ax=axs[1],
)
fig = plt.figure()
axs = heputils.plot.stack_ratio_plot(
simulation_hists,
data_hist=data_hist,
ratio_numerator="data", # "data", "simulation", or "mc"
labels=labels,
color=colormap,
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
rp_uncert_draw_type="line", # "line" or "bar"
)
fig.savefig("example_stack_ratio.png")
fig = plt.figure()
axs = heputils.plot.stack_ratio_plot(
simulation_hists,
data_hist=data_hist,
labels=labels,
color=colormap,
rp_ylim=[-1, 8],
xlabel=r"$X$ Mass [GeV]",
ylabel="Count",
logy=False,
rp_uncert_draw_type="bar",
)
stacked_hist = heputils.utils.sum_hists(simulation_hists)
stacked_hist.plot();
stacked_hist