13
13
import numpy as np
14
14
import plotext as plt
15
15
import uproot
16
+ import uproot .behaviors .TH1
17
+ import uproot .models .RNTuple
16
18
17
19
from uproot_browser .exceptions import EmptyTreeError
18
20
@@ -42,7 +44,7 @@ def make_hist_title(item: Any, histogram: hist.Hist) -> str:
42
44
43
45
44
46
@functools .singledispatch
45
- def plot (tree : Any , * , expr : str = "" ) -> None : # noqa: ARG001
47
+ def plot (tree : Any , * , width : int = 100 , expr : str = "" ) -> None : # noqa: ARG001
46
48
"""
47
49
Implement this for each type of plottable.
48
50
"""
@@ -53,7 +55,10 @@ def plot(tree: Any, *, expr: str = "") -> None: # noqa: ARG001
53
55
# Simpler in Python 3.11+
54
56
@plot .register (uproot .TBranch )
55
57
def plot_branch (
56
- tree : uproot .TBranch | uproot .models .RNTuple .RField , * , expr : str = ""
58
+ tree : uproot .TBranch | uproot .models .RNTuple .RField ,
59
+ * ,
60
+ width : int = 100 ,
61
+ expr : str = "" ,
57
62
) -> None :
58
63
"""
59
64
Plot a single tree branch.
@@ -64,14 +69,13 @@ def plot_branch(
64
69
if len (finite ) < 1 :
65
70
msg = f"Branch { tree .name } is empty."
66
71
raise EmptyTreeError (msg )
67
- histogram : hist .Hist = hist .numpy .histogram (finite , bins = 100 , histogram = hist .Hist )
72
+ histogram : hist .Hist = hist .numpy .histogram (finite , bins = width , histogram = hist .Hist )
68
73
if expr :
69
74
# pylint: disable-next=eval-used
70
75
histogram = eval (expr , {"h" : histogram })
71
76
plt .bar (
72
- histogram .axes [0 ].centers ,
77
+ histogram .axes [0 ].edges ,
73
78
histogram .values ().astype (float ),
74
- width = histogram .axes [0 ].widths ,
75
79
)
76
80
plt .ylim (lower = 0 )
77
81
plt .xticks (np .linspace (histogram .axes [0 ][0 ][0 ], histogram .axes [0 ][- 1 ][- 1 ], 5 ))
@@ -83,15 +87,19 @@ def plot_branch(
83
87
84
88
85
89
@plot .register
86
- def plot_hist (tree : uproot .behaviors .TH1 .Histogram , expr : str = "" ) -> None :
90
+ def plot_hist (
91
+ tree : uproot .behaviors .TH1 .Histogram ,
92
+ width : int = 100 , # noqa: ARG001
93
+ expr : str = "" ,
94
+ ) -> None :
87
95
"""
88
96
Plot a 1-D Histogram.
89
97
"""
90
98
histogram = hist .Hist (tree .to_hist ())
91
99
if expr :
92
100
# pylint: disable-next=eval-used
93
101
histogram = eval (expr , {"h" : histogram })
94
- plt .bar (histogram .axes [0 ].centers , histogram .values ().astype (float ))
102
+ plt .bar (histogram .axes [0 ].edges , histogram .values ().astype (float ))
95
103
plt .ylim (lower = 0 )
96
104
plt .xticks (np .linspace (histogram .axes [0 ][0 ][0 ], histogram .axes [0 ][- 1 ][- 1 ], 5 ))
97
105
plt .xlabel (histogram .axes [0 ].name )
0 commit comments