Skip to content

Commit eb66411

Browse files
committed
fix: better plotting
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 5666181 commit eb66411

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/uproot_browser/plot.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import numpy as np
1414
import plotext as plt
1515
import uproot
16+
import uproot.behaviors.TH1
17+
import uproot.models.RNTuple
1618

1719
from 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)
5557
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 = "",
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)

src/uproot_browser/tui/plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def make_plot(item: Any, theme: str, *size: int, expr: str) -> Any:
3131
plt.clf()
3232
plt.theme(theme)
3333
plt.plotsize(*size)
34-
uproot_browser.plot.plot(item, expr=expr)
34+
uproot_browser.plot.plot(item, width=(size[0] - 5) * 4, expr=expr)
3535
return plt.build()
3636

3737

0 commit comments

Comments
 (0)