Skip to content

Commit

Permalink
fix: overflow bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ming-Yan committed Aug 8, 2023
1 parent 9b47e92 commit fe24f8d
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/mplhep/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def histplot(
elif flow == "hint":
plottables.append(Plottable(value, edges=final_bins, variances=variance))
elif flow == "show":
if underflow > 0:
if underflow != 0:
if i == 0:
flow_bins = np.insert(
final_bins,
Expand All @@ -249,7 +249,7 @@ def histplot(
value, variance = np.insert(
value, 0, h.values(flow=True)[0]
), np.insert(value, 0, h.variances(flow=True)[0])
if overflow > 0:
if overflow != 0:
if i == 0:
flow_bins = np.append(
flow_bins,
Expand Down Expand Up @@ -509,7 +509,7 @@ def iterable_not_string(arg):
if x_axes_label:
ax.set_xlabel(x_axes_label)

if flow in {"hint", "show"} and (underflow > 0.0 or overflow > 0.0):
if flow in {"hint", "show"} and (underflow != 0.0 or overflow != 0.0):
d = 0.9 # proportion of vertical to horizontal extent of the slanted line
trans = mpl.transforms.blended_transform_factory(ax.transData, ax.transAxes)
ax_h = ax.bbox.height
Expand All @@ -524,7 +524,7 @@ def iterable_not_string(arg):
transform=trans,
)
xticks = ax.get_xticks().tolist()
if underflow > 0.0:
if underflow != 0.0:
if flow == "hint":
ax.plot(
[
Expand All @@ -544,7 +544,7 @@ def iterable_not_string(arg):
xticks[1] = f"<{flow_bins[2]}"

ax.set_xticklabels(xticks)
if overflow > 0.0:
if overflow != 0.0:
if flow == "hint":
ax.plot(
[
Expand Down Expand Up @@ -668,47 +668,50 @@ def hist2dplot(
print(f"Warning: you don't have flow bins stored in {h}")
elif flow == "show":
H = h.values(flow=True)
if any(h.values(flow=True)[0] > 0):
if any(h.values(flow=True)[0] != 0):
xbins = np.array(
[
xbins[0] - (xbins[-1] - xbins[0]) * 0.08,
xbins[0] - (xbins[-1] - xbins[0]) * 0.03,
*xbins,
]
)
if any(h.values(flow=True)[-1] > 0):
H = np.insert(H, (1), np.nan, axis=-1)
else:
H = H[1:]
if any(h.values(flow=True)[-1] != 0):
xbins = np.array(
[
*xbins,
xbins[-1] + (xbins[-1] - xbins[0]) * 0.03,
xbins[-1] + (xbins[-1] - xbins[0]) * 0.08,
]
)
if any(h.values(flow=True)[:, 0] > 0):
H = np.insert(H, (-1), np.nan, axis=-1)
else:
H = H[:-1]
if any(h.values(flow=True)[:, 0] != 0):
ybins = np.array(
[
ybins[0] - (ybins[-1] - ybins[0]) * 0.08,
ybins[0] - (ybins[-1] - ybins[0]) * 0.03,
*ybins,
]
)
if any(h.values(flow=True)[:, -1] > 0):
H = np.insert(H, (1), np.full(np.shape(H)[1], np.nan), axis=0)
else:
H = H[:, 1:]
if any(h.values(flow=True)[:, -1] != 0):
ybins = np.array(
[
*ybins,
ybins[-1] + (ybins[-1] - ybins[0]) * 0.03,
ybins[-1] + (ybins[-1] - ybins[0]) * 0.08,
]
)

if any(h.values(flow=True)[0] > 0.0):
H = np.insert(H, (1), np.nan, axis=-1)
if any(h.values(flow=True)[-1] > 0.0):
H = np.insert(H, (-1), np.nan, axis=-1)
if any(h.values(flow=True)[:, 0] > 0):
H = np.insert(H, (1), np.full(np.shape(H)[1], np.nan), axis=0)
if any(h.values(flow=True)[:, -1] > 0):
H = np.insert(H, (-1), np.full(np.shape(H)[1], np.nan), axis=0)
else:
H = H[:, :-1]
elif flow == "sum":
H = h.values().copy()
# Sum borders
Expand Down Expand Up @@ -794,7 +797,7 @@ def hist2dplot(
mew=1,
clip_on=False,
)
if any(h.values(flow=True)[0] > 0):
if any(h.values(flow=True)[0] != 0):
if flow == "hint":
ax.plot(
[
Expand All @@ -808,7 +811,7 @@ def hist2dplot(
if flow == "show":
ax.plot([xbins[1], xbins[2]], [0, 0], transform=trans, **kwargs)
ax.plot([xbins[0], xbins[0]], [ybins[1], ybins[2]], **kwargs)
if any(h.values(flow=True)[:, 0] > 0):
if any(h.values(flow=True)[:, 0] != 0):
if flow == "hint":
ax.plot(
[
Expand All @@ -822,7 +825,7 @@ def hist2dplot(
if flow == "show":
ax.plot([xbins[-3], xbins[-2]], [0, 0], transform=trans, **kwargs)
ax.plot([xbins[-1], xbins[-1]], [ybins[1], ybins[2]], **kwargs)
if any(h.values(flow=True)[-1] > 0):
if any(h.values(flow=True)[-1] != 0):
if flow == "hint":
ax.plot(
[
Expand All @@ -837,7 +840,7 @@ def hist2dplot(
ax.plot([xbins[1], xbins[2]], [1, 1], transform=trans, **kwargs)
ax.plot([xbins[0], xbins[0]], [ybins[-3], ybins[-2]], **kwargs)

if any(h.values(flow=True)[:, -1] > 0):
if any(h.values(flow=True)[:, -1] != 0):
if flow == "hint":
ax.plot(
[
Expand Down

0 comments on commit fe24f8d

Please sign in to comment.