2
2
3
3
import collections .abc
4
4
import inspect
5
+ import warnings
5
6
from collections import OrderedDict , namedtuple
6
7
from typing import TYPE_CHECKING , Any , Union
7
8
@@ -132,7 +133,7 @@ def histplot(
132
133
Attempts to draw x-axis ticks coinciding with bin boundaries if feasible.
133
134
ax : matplotlib.axes.Axes, optional
134
135
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" }
136
137
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.
137
138
**kwargs :
138
139
Keyword arguments passed to underlying matplotlib functions -
@@ -154,6 +155,12 @@ def histplot(
154
155
_allowed_histtype = ["fill" , "step" , "errorbar" ]
155
156
_err_message = f"Select 'histtype' from: { _allowed_histtype } "
156
157
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"
157
164
158
165
# Convert 1/0 etc to real bools
159
166
stack = bool (stack )
@@ -204,25 +211,22 @@ def histplot(
204
211
flow_bins = final_bins
205
212
for i , h in enumerate (hists ):
206
213
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 :
212
215
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
+ )
215
219
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
217
221
elif (
218
222
hasattr (h , "axes" )
219
223
and hasattr (h .axes [0 ], "traits" )
220
224
and hasattr (h .axes [0 ].traits , "underflow" )
221
225
and not h .axes [0 ].traits .underflow
222
226
and not h .axes [0 ].traits .overflow
227
+ and flow in {"show" , "sum" }
223
228
):
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 )
226
230
plottables .append (Plottable (value , edges = final_bins , variances = variance ))
227
231
elif flow == "hint" :
228
232
plottables .append (Plottable (value , edges = final_bins , variances = variance ))
@@ -505,7 +509,8 @@ def iterable_not_string(arg):
505
509
506
510
if x_axes_label :
507
511
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 ):
509
514
d = 0.9 # proportion of vertical to horizontal extent of the slanted line
510
515
trans = mpl .transforms .blended_transform_factory (ax .transData , ax .transAxes )
511
516
ax_h = ax .bbox .height
0 commit comments