@@ -426,58 +426,53 @@ def plot_hist_wrapper(*args, **kwargs) -> dict[str, Any]:
426426 results = func (* args , ** kwargs )
427427
428428 fig = go .Figure ()
429- data_all = []
429+
430430 for model_name , hist_data in results .items ():
431- # Create figure
432- for point in hist_data :
433- data_all .append (point )
434- if bins is None or isinstance (bins , int ) or isinstance (bins , float ):
435- fig .add_trace (
436- go .Histogram (
437- x = hist_data ,
438- histnorm = "probability density" ,
439- nbinsx = bins ,
440- name = model_name ,
441- )
431+ # Construct bin edges
432+ if isinstance (bins , dict ):
433+ edges = np .arange (
434+ bins ["start" ],
435+ bins ["end" ] + bins ["size" ],
436+ bins ["size" ],
442437 )
443438 else :
444- fig .add_trace (
445- go .Histogram (
446- x = hist_data ,
447- histnorm = "probability density" ,
448- xbins = bins ,
449- autobinx = False ,
450- name = model_name ,
451- )
439+ edges = np .histogram_bin_edges (hist_data , bins = bins )
440+
441+ # Compute probability density histogram
442+ counts , edges = np .histogram (hist_data , bins = edges , density = True )
443+
444+ centres = 0.5 * (edges [:- 1 ] + edges [1 :])
445+ widths = np .diff (edges )
446+
447+ # Decide colour of each bar
448+ colours = []
449+ if good is None or bad is None :
450+ colours = ["#276419" ] * len (centres )
451+ else :
452+ colours = [
453+ "#276419" if good <= c <= bad else "#D73027" for c in centres
454+ ]
455+
456+ fig .add_trace (
457+ go .Bar (
458+ x = centres ,
459+ y = counts ,
460+ width = widths ,
461+ marker_color = colours ,
462+ name = model_name ,
452463 )
464+ )
453465
454- if good is not None and bad is not None and isinstance (bins , dict ):
455- actual_bins = [min (data_all )]
456- point = actual_bins [0 ]
457- while point < max (data_all ):
458- point += bins ["size" ]
459- actual_bins .append (point )
460- colors = np .zeros_like (actual_bins )
461- bad_exists = False
462- for i , point in enumerate (actual_bins ):
463- if point < good or point > bad :
464- bad_exists = True
465- colors [i ] = bins ["start" ]
466- else :
467- colors [i ] = bins ["end" ]
468- if not bad_exists :
469- colors = "#276419"
470- fig .update_traces (marker_color = colors )
471- # Update layout
472466 fig .update_layout (
473- title = {"text" : title },
474- xaxis = {"title" : {"text" : x_label }},
475- yaxis = {"title" : {"text" : y_label }},
467+ barmode = "overlay" ,
468+ title = title ,
469+ xaxis_title = x_label ,
470+ yaxis_title = y_label ,
476471 )
477472
478- fig .update_traces ()
473+ if isinstance (bins , dict ):
474+ fig .update_xaxes (range = [bins ["start" ], bins ["end" ]])
479475
480- # Write to file
481476 Path (filename ).parent .mkdir (parents = True , exist_ok = True )
482477 fig .write_json (filename )
483478
0 commit comments