@@ -145,6 +145,7 @@ def get_plottables(
145
145
stack = False ,
146
146
density = False ,
147
147
binwnorm = None ,
148
+ xoffsets = False ,
148
149
):
149
150
"""
150
151
Generate plottable histograms from various histogram data sources.
@@ -189,6 +190,8 @@ def get_plottables(
189
190
binwnorm : float, optional
190
191
If true, convert sum weights to bin-width-normalized, with unit equal to
191
192
supplied value (usually you want to specify 1.)
193
+ xoffsets : bool | float | iterable, optional
194
+ Offset for x-axis values.
192
195
193
196
Returns
194
197
-------
@@ -203,7 +206,16 @@ def get_plottables(
203
206
hists = list (process_histogram_parts (H , bins ))
204
207
final_bins , _ = get_plottable_protocol_bins (hists [0 ].axes [0 ])
205
208
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 ):
207
219
value , variance = np .copy (h .values ()), h .variances ()
208
220
if has_variances := variance is not None :
209
221
variance = np .copy (variance )
@@ -241,7 +253,9 @@ def get_plottables(
241
253
242
254
# Set plottables
243
255
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
+ )
245
259
elif flow == "show" :
246
260
_flow_bin_size : float = np .max (
247
261
[0.05 * (final_bins [- 1 ] - final_bins [0 ]), np .mean (np .diff (final_bins ))]
@@ -257,7 +271,9 @@ def get_plottables(
257
271
value = np .r_ [value , overflow ]
258
272
if has_variances :
259
273
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
+ )
261
277
elif flow == "sum" :
262
278
if underflow > 0 :
263
279
value [0 ] += underflow
@@ -267,9 +283,13 @@ def get_plottables(
267
283
value [- 1 ] += overflow
268
284
if has_variances :
269
285
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
+ )
271
289
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
+ )
273
293
274
294
if w2 is not None :
275
295
for _w2 , _plottable in zip (
@@ -422,7 +442,14 @@ def norm_stack_plottables(plottables, bins, stack=False, density=False, binwnorm
422
442
423
443
class Plottable :
424
444
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" ,
426
453
):
427
454
self ._values = np .array (values ).astype (float )
428
455
self .variances = None
@@ -440,8 +467,14 @@ def __init__(
440
467
if self .edges is None :
441
468
self .edges = np .arange (len (values ) + 1 )
442
469
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
+ )
444
476
477
+ self .method = w2method
445
478
self .yerr = yerr
446
479
assert self .variances is None or self .yerr is None
447
480
if self .yerr is not None :
@@ -579,6 +612,7 @@ def to_errorbar(self):
579
612
"x" : self .centers ,
580
613
"y" : self .values ,
581
614
"yerr" : [self .yerr_lo , self .yerr_hi ],
615
+ "xerr" : [self .xerr_lo , self .xerr_hi ],
582
616
}
583
617
584
618
0 commit comments