Difference

1D Comparison Difference

Imports
import hist
import numpy as np
from hist import Hist

import mplhep as mh

np.random.seed(42)
Setup
# Generate dummy data
x1 = np.r_[np.random.normal(0.4, 0.1, 5000), np.random.normal(0.7, 0.1, 5000)]
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms"
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight())  # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight()  # Shorthand interface
h1.fill(x1)
h2.fill(x2)

Code

from mplhep import add_text

fig, ax_main, ax_comparison = mh.comp.hists(
    h1,
    h2,
    xlabel="Variable",
    ylabel="Entries",
    h1_label=r"$h_1$",
    h2_label=r"$h_2$",
    comparison="difference",  # <--
)

add_text(
    "Comparison of two hist with difference plot",
    ax=ax_main,
    fontsize="small",
    loc="over left",
)
add_text("Difference ax", ax=ax_comparison, loc="over right", fontsize="small")
Full code
import hist
import numpy as np
from hist import Hist

import mplhep as mh

np.random.seed(42)

# Generate dummy data
x1 = np.r_[np.random.normal(0.4, 0.1, 5000), np.random.normal(0.7, 0.1, 5000)]
x2 = np.r_[np.random.normal(0.4, 0.1, 1000), np.random.normal(0.7, 0.11, 7000)]

# Create and fill histograms"
h1 = Hist(hist.axis.Regular(50, 0, 1), storage=hist.storage.Weight())  # Long interface
h2 = hist.new.Regular(50, 0, 1).Weight()  # Shorthand interface
h1.fill(x1)
h2.fill(x2)


from mplhep import add_text

fig, ax_main, ax_comparison = mh.comp.hists(
    h1,
    h2,
    xlabel="Variable",
    ylabel="Entries",
    h1_label=r"$h_1$",
    h2_label=r"$h_2$",
    comparison="difference",  # <--
)

add_text(
    "Comparison of two hist with difference plot",
    ax=ax_main,
    fontsize="small",
    loc="over left",
)
add_text("Difference ax", ax=ax_comparison, loc="over right", fontsize="small")