norm_to_integral#

Hist2d.norm_to_integral()[source]#

Normalize histogram to it’s integral.

The integral is calculated as the sum of all bins weighted by their area.

Returns:
normalized_hist2dHist2d

The normalized histogram.

Examples

import matplotlib.patheffects as patheffects
import matplotlib.pyplot as plt
import numpy as np

import atompy as ap

plt.style.use("atom")
plt.rcParams["axes.grid"] = False

# create a Histogram
xedges = np.array([0, 1, 2, 3])
yedges = np.array([0, 1, 3, 6, 10])
values = np.array(
    [
        [0, 1, 2, 3],
        [4, 5, 6, 7],
        [8, 9, 10, 11],
    ]
)
hist = ap.Hist2d(values, xedges, yedges)
hists = [
    hist,
    hist.norm_to_integral(),
    hist.norm_to_max(),
    hist.norm_to_sum(),
]

# plot different normalizations
titles = "original", "norm. to integral", "norm. to max", "norm. to sum"

fig, axs = plt.subplots(1, 4, layout="compressed", figsize=(12, 3))

for i, ax in enumerate(axs):
    ax.set_title(titles[i])
    ax.set_box_aspect(1)
    hists[i].plot(ax=ax, title=titles[i])

    # show values of bin in plot
    for j, x in enumerate(hists[i].xcenters):
        for k, y in enumerate(hists[i].ycenters):
            text = ax.text(
                x, y, f"{hists[i].values[j, k]:.2f}", va="center", ha="center", c="w"
            )
            text.set_path_effects(
                [
                    patheffects.withStroke(linewidth=1.5, foreground="k"),
                    patheffects.Normal(),
                ]
            )

plt.show()

(Source code, png, hires.png, pdf)

../../../_images/normalize.png