Skip to content

Commit bd90705

Browse files
authored
fix: flow warning fix (#426)
* fix: flow default and warning fix Signed-off-by: Henry Schreiner <[email protected]> * fix: match description in PR Signed-off-by: Henry Schreiner <[email protected]> * fix: only compute transform if needed Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent c4ebc16 commit bd90705

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/mplhep/plot.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import collections.abc
44
import inspect
5+
import warnings
56
from collections import OrderedDict, namedtuple
67
from typing import TYPE_CHECKING, Any, Union
78

@@ -132,7 +133,7 @@ def histplot(
132133
Attempts to draw x-axis ticks coinciding with bin boundaries if feasible.
133134
ax : matplotlib.axes.Axes, optional
134135
Axes object (if None, last one is fetched or one is created)
135-
flow : str, optional { "show", "sum", "hint", None}
136+
flow : str, optional { "show", "sum", "hint", "none"}
136137
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.
137138
**kwargs :
138139
Keyword arguments passed to underlying matplotlib functions -
@@ -154,6 +155,12 @@ def histplot(
154155
_allowed_histtype = ["fill", "step", "errorbar"]
155156
_err_message = f"Select 'histtype' from: {_allowed_histtype}"
156157
assert histtype in _allowed_histtype, _err_message
158+
assert flow is None or flow in {
159+
"show",
160+
"sum",
161+
"hint",
162+
"none",
163+
}, "flow must be show, sum, hint, or none"
157164

158165
# Convert 1/0 etc to real bools
159166
stack = bool(stack)
@@ -204,25 +211,22 @@ def histplot(
204211
flow_bins = final_bins
205212
for i, h in enumerate(hists):
206213
value, variance = h.values(), h.variances()
207-
if (
208-
hasattr(h, "values")
209-
and "flow" not in inspect.getfullargspec(h.values).args
210-
and flow is not None
211-
):
214+
if hasattr(h, "values") and "flow" not in inspect.getfullargspec(h.values).args:
212215
if flow == "sum" or flow == "show":
213-
print(f"Warning: {type(h)} is not allowed to get flow bins")
214-
flow = None
216+
warnings.warn(
217+
f"{type(h)} is not allowed to get flow bins", stacklevel=2
218+
)
215219
plottables.append(Plottable(value, edges=final_bins, variances=variance))
216-
# check the original hist as flow bins
220+
# check if the original hist has flow bins
217221
elif (
218222
hasattr(h, "axes")
219223
and hasattr(h.axes[0], "traits")
220224
and hasattr(h.axes[0].traits, "underflow")
221225
and not h.axes[0].traits.underflow
222226
and not h.axes[0].traits.overflow
227+
and flow in {"show", "sum"}
223228
):
224-
print(f"Warning: you don't have flow bins stored in {h}")
225-
flow = None
229+
warnings.warn(f"You don't have flow bins stored in {h!r}", stacklevel=2)
226230
plottables.append(Plottable(value, edges=final_bins, variances=variance))
227231
elif flow == "hint":
228232
plottables.append(Plottable(value, edges=final_bins, variances=variance))
@@ -505,7 +509,8 @@ def iterable_not_string(arg):
505509

506510
if x_axes_label:
507511
ax.set_xlabel(x_axes_label)
508-
if flow == "hint" or flow == "show":
512+
513+
if flow in {"hint", "show"} and (underflow > 0.0 or overflow > 0.0):
509514
d = 0.9 # proportion of vertical to horizontal extent of the slanted line
510515
trans = mpl.transforms.blended_transform_factory(ax.transData, ax.transAxes)
511516
ax_h = ax.bbox.height

0 commit comments

Comments
 (0)