@@ -145,6 +145,7 @@ def get_plottables(
145145 stack = False ,
146146 density = False ,
147147 binwnorm = None ,
148+ xoffsets = False ,
148149):
149150 """
150151 Generate plottable histograms from various histogram data sources.
@@ -189,6 +190,8 @@ def get_plottables(
189190 binwnorm : float, optional
190191 If true, convert sum weights to bin-width-normalized, with unit equal to
191192 supplied value (usually you want to specify 1.)
193+ xoffsets : bool | float | iterable, optional
194+ Offset for x-axis values.
192195
193196 Returns
194197 -------
@@ -203,7 +206,16 @@ def get_plottables(
203206 hists = list (process_histogram_parts (H , bins ))
204207 final_bins , _ = get_plottable_protocol_bins (hists [0 ].axes [0 ])
205208
206- for h in hists :
209+ if xoffsets is True :
210+ parsed_offsets = []
211+ widths = np .diff (final_bins )
212+ sub_bin_width = widths / (len (hists ) + 1 )
213+ for i in range (len (hists )):
214+ parsed_offsets .append (sub_bin_width * (i + 1 ))
215+ xoffsets = parsed_offsets
216+ else :
217+ xoffsets = [None ] * len (hists )
218+ for h , xoffset in zip (hists , xoffsets ):
207219 value , variance = np .copy (h .values ()), h .variances ()
208220 if has_variances := variance is not None :
209221 variance = np .copy (variance )
@@ -241,7 +253,9 @@ def get_plottables(
241253
242254 # Set plottables
243255 if flow in ("none" , "hint" ):
244- plottables .append (Plottable (value , edges = final_bins , variances = variance ))
256+ plottables .append (
257+ Plottable (value , edges = final_bins , variances = variance , xoffsets = xoffset )
258+ )
245259 elif flow == "show" :
246260 _flow_bin_size : float = np .max (
247261 [0.05 * (final_bins [- 1 ] - final_bins [0 ]), np .mean (np .diff (final_bins ))]
@@ -257,7 +271,9 @@ def get_plottables(
257271 value = np .r_ [value , overflow ]
258272 if has_variances :
259273 variance = np .r_ [variance , overflowv ]
260- plottables .append (Plottable (value , edges = flow_bins , variances = variance ))
274+ plottables .append (
275+ Plottable (value , edges = flow_bins , variances = variance , xoffsets = xoffset )
276+ )
261277 elif flow == "sum" :
262278 if underflow > 0 :
263279 value [0 ] += underflow
@@ -267,9 +283,13 @@ def get_plottables(
267283 value [- 1 ] += overflow
268284 if has_variances :
269285 variance [- 1 ] += overflowv
270- plottables .append (Plottable (value , edges = final_bins , variances = variance ))
286+ plottables .append (
287+ Plottable (value , edges = final_bins , variances = variance , xoffsets = xoffset )
288+ )
271289 else :
272- plottables .append (Plottable (value , edges = final_bins , variances = variance ))
290+ plottables .append (
291+ Plottable (value , edges = final_bins , variances = variance , xoffsets = xoffset )
292+ )
273293
274294 if w2 is not None :
275295 for _w2 , _plottable in zip (
@@ -422,7 +442,14 @@ def norm_stack_plottables(plottables, bins, stack=False, density=False, binwnorm
422442
423443class Plottable :
424444 def __init__ (
425- self , values , * , edges = None , variances = None , yerr = None , w2method = "poisson"
445+ self ,
446+ values ,
447+ * ,
448+ edges = None ,
449+ xoffsets = None ,
450+ variances = None ,
451+ yerr = None ,
452+ w2method = "poisson" ,
426453 ):
427454 self ._values = np .array (values ).astype (float )
428455 self .variances = None
@@ -440,8 +467,14 @@ def __init__(
440467 if self .edges is None :
441468 self .edges = np .arange (len (values ) + 1 )
442469 self .centers = self .edges [:- 1 ] + np .diff (self .edges ) / 2
443- self .method = w2method
470+ if xoffsets is not None :
471+ self .centers = self .edges [:- 1 ] + xoffsets
472+ self .xerr_lo , self .xerr_hi = (
473+ self .centers - self .edges [:- 1 ],
474+ self .edges [1 :] - self .centers ,
475+ )
444476
477+ self .method = w2method
445478 self .yerr = yerr
446479 assert self .variances is None or self .yerr is None
447480 if self .yerr is not None :
@@ -579,6 +612,7 @@ def to_errorbar(self):
579612 "x" : self .centers ,
580613 "y" : self .values ,
581614 "yerr" : [self .yerr_lo , self .yerr_hi ],
615+ "xerr" : [self .xerr_lo , self .xerr_hi ],
582616 }
583617
584618
0 commit comments