Skip to content

Commit 0cdd4c1

Browse files
authored
feat: binwnorm for hist2dplot (#538)
Closes: #525
1 parent fb2a355 commit 0cdd4c1

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/mplhep/plot.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ def hist2dplot(
664664
cmax=None,
665665
ax: mpl.axes.Axes | None = None,
666666
flow="hint",
667+
binwnorm=None,
667668
**kwargs,
668669
):
669670
"""
@@ -706,6 +707,9 @@ def hist2dplot(
706707
Axes object (if None, last one is fetched or one is created)
707708
flow : str, optional {"show", "sum","hint", None}
708709
Whether plot the under/overflow bin. If "show", add additional under/overflow bin. If "sum", add the under/overflow bin content to first/last bin. "hint" would highlight the bins with under/overflow contents
710+
binwnorm : float, optional
711+
If true, convert sum weights to bin-width-normalized, with unit equal to
712+
supplied value (usually you want to specify 1.)
709713
**kwargs :
710714
Keyword arguments passed to underlying matplotlib function - pcolormesh.
711715
@@ -809,6 +813,15 @@ def hist2dplot(
809813

810814
X, Y = np.meshgrid(xbins, ybins)
811815

816+
if binwnorm is not None:
817+
# No error treatment so we can just scale the values
818+
H = H * binwnorm
819+
# Make sure x_bin_width and y_bin_width align with H's dimensions
820+
X_bin_widths, Y_bin_widths = np.meshgrid(np.diff(xbins), np.diff(ybins))
821+
# Calculate the bin area array, which aligns with the shape of H
822+
bin_area = X_bin_widths * Y_bin_widths
823+
H = H / bin_area
824+
812825
kwargs.setdefault("shading", "flat")
813826
pc = ax.pcolormesh(X, Y, H, vmin=cmin, vmax=cmax, **kwargs)
814827

0 commit comments

Comments
 (0)