@@ -279,17 +279,26 @@ def comparison(
279279 if flow == "show" :
280280 # Try to get flow bins if they exist (from original histogram objects, not plottable)
281281 try :
282- # Access flow bins from the original histogram objects
283- h1_flow_values = h1 .values (flow = True )
284-
285- # Check if histogram actually has flow bins (length should be +2)
286- if len (h1_flow_values ) == len (h1_plottable .values ()) + 2 :
287- # Use the original histograms which already have flow bins
288- h1_for_comparison = h1
289- h2_for_comparison = h2
290- used_flow_bins = True
282+ # First check if both histograms support flow=True without actually calling it
283+ # This prevents side effects from accessing flow values on histograms that don't have them
284+ if (hasattr (h1 , 'values' ) and hasattr (h2 , 'values' ) and
285+ hasattr (h1 .values , '__call__' ) and hasattr (h2 .values , '__call__' )):
286+
287+ # Access flow bins from the original histogram objects
288+ h1_flow_values = h1 .values (flow = True )
289+
290+ # Check if histogram actually has flow bins (length should be +2)
291+ if len (h1_flow_values ) == len (h1_plottable .values ()) + 2 :
292+ # Use the original histograms which already have flow bins
293+ h1_for_comparison = h1
294+ h2_for_comparison = h2
295+ used_flow_bins = True
296+ else :
297+ # No actual flow bins, use regular histograms
298+ h1_for_comparison = h1_plottable
299+ h2_for_comparison = h2_plottable
291300 else :
292- # No actual flow bins, use regular histograms
301+ # Histograms don't support flow parameter
293302 h1_for_comparison = h1_plottable
294303 h2_for_comparison = h2_plottable
295304 except (AttributeError , TypeError ):
@@ -349,13 +358,15 @@ def comparison(
349358 0.05 * (final_bins [- 1 ] - final_bins [0 ]), np .mean (np .diff (final_bins ))
350359 )
351360
352- # Check if underflow/overflow bins exist and extend edges accordingly
361+ # For flow="show", we need to always extend edges to match flow values length
362+ # because flow=True always includes underflow/overflow positions
353363 flow_edges = np .copy (final_bins )
354364 h2_flow_values = h2_for_comparison .values (flow = True )
355- if h2_flow_values [0 ] > 0 : # Underflow exists
356- flow_edges = np .insert (flow_edges , 0 , flow_edges [0 ] - _flow_bin_size )
357- if h2_flow_values [- 1 ] > 0 : # Overflow exists
358- flow_edges = np .append (flow_edges , flow_edges [- 1 ] + _flow_bin_size )
365+
366+ # Always add underflow and overflow edges when using flow="show"
367+ # to match the structure of flow=True values
368+ flow_edges = np .insert (flow_edges , 0 , flow_edges [0 ] - _flow_bin_size )
369+ flow_edges = np .append (flow_edges , flow_edges [- 1 ] + _flow_bin_size )
359370
360371 comparison_plottable = EnhancedPlottableHistogram (
361372 comparison_values ,
@@ -389,14 +400,20 @@ def comparison(
389400 if hasattr (comparison_plottable , "errors" ):
390401 comparison_plottable .errors ()
391402
403+ # Filter out comparison-specific parameters that shouldn't be passed to histplot
404+ _valid_histplot_kwargs = {
405+ k : v for k , v in histplot_kwargs .items ()
406+ if k not in ['ratio' , 'comparison' , 'comparison_ylabel' , 'comparison_ylim' ]
407+ }
408+
392409 if comparison == "pull" :
393- histplot_kwargs .setdefault ("histtype" , "fill" )
394- histplot_kwargs .setdefault ("color" , "darkgrey" )
395- histplot (comparison_plottable , ax = ax , flow = flow , ** histplot_kwargs )
410+ _valid_histplot_kwargs .setdefault ("histtype" , "fill" )
411+ _valid_histplot_kwargs .setdefault ("color" , "darkgrey" )
412+ histplot (comparison_plottable , ax = ax , flow = flow , ** _valid_histplot_kwargs )
396413 else :
397- histplot_kwargs .setdefault ("color" , "black" )
398- histplot_kwargs .setdefault ("histtype" , "errorbar" )
399- histplot (comparison_plottable , ax = ax , flow = flow , ** histplot_kwargs )
414+ _valid_histplot_kwargs .setdefault ("color" , "black" )
415+ _valid_histplot_kwargs .setdefault ("histtype" , "errorbar" )
416+ histplot (comparison_plottable , ax = ax , flow = flow , ** _valid_histplot_kwargs )
400417
401418 if comparison in ["ratio" , "split_ratio" , "relative_difference" ]:
402419 if comparison_ylim is None :
0 commit comments