1313import numpy as np
1414import plotext as plt
1515import uproot
16+ import uproot .behaviors .TH1
17+ import uproot .models .RNTuple
1618
1719from uproot_browser .exceptions import EmptyTreeError
1820
@@ -42,7 +44,7 @@ def make_hist_title(item: Any, histogram: hist.Hist) -> str:
4244
4345
4446@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
4648 """
4749 Implement this for each type of plottable.
4850 """
@@ -53,7 +55,10 @@ def plot(tree: Any, *, expr: str = "") -> None: # noqa: ARG001
5355# Simpler in Python 3.11+
5456@plot .register (uproot .TBranch )
5557def 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 = "" ,
5762) -> None :
5863 """
5964 Plot a single tree branch.
@@ -64,12 +69,12 @@ def plot_branch(
6469 if len (finite ) < 1 :
6570 msg = f"Branch { tree .name } is empty."
6671 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 )
6873 if expr :
6974 # pylint: disable-next=eval-used
7075 histogram = eval (expr , {"h" : histogram })
7176 plt .bar (
72- histogram .axes [0 ].centers ,
77+ histogram .axes [0 ].edges ,
7378 histogram .values ().astype (float ),
7479 )
7580 plt .ylim (lower = 0 )
@@ -82,15 +87,19 @@ def plot_branch(
8287
8388
8489@plot .register
85- 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 :
8695 """
8796 Plot a 1-D Histogram.
8897 """
8998 histogram = hist .Hist (tree .to_hist ())
9099 if expr :
91100 # pylint: disable-next=eval-used
92101 histogram = eval (expr , {"h" : histogram })
93- plt .bar (histogram .axes [0 ].centers , histogram .values ().astype (float ))
102+ plt .bar (histogram .axes [0 ].edges , histogram .values ().astype (float ))
94103 plt .ylim (lower = 0 )
95104 plt .xticks (np .linspace (histogram .axes [0 ][0 ][0 ], histogram .axes [0 ][- 1 ][- 1 ], 5 ))
96105 plt .xlabel (histogram .axes [0 ].name )
0 commit comments