-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplot_mpl.py
48 lines (38 loc) · 1.06 KB
/
plot_mpl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Display tools for making plots via plotext.
"""
from __future__ import annotations
import functools
from typing import Any
import awkward as ak
import hist
import matplotlib.pyplot as plt
import uproot
import uproot.behaviors.TH1
import uproot_browser.plot
@functools.singledispatch
def plot(tree: Any) -> None: # noqa: ARG001
"""
Implement this for each type of plottable.
"""
msg = "This object is not plottable yet"
raise RuntimeError(msg)
@plot.register
def plot_branch(tree: uproot.TBranch) -> None:
"""
Plot a single tree branch.
"""
array = tree.array()
histogram: hist.Hist = hist.numpy.histogram(
ak.flatten(array) if array.ndim > 1 else array, bins=50, histogram=hist.Hist
)
histogram.plot()
plt.title(uproot_browser.plot.make_hist_title(tree, histogram))
@plot.register
def plot_hist(tree: uproot.behaviors.TH1.Histogram) -> None:
"""
Plot a 1-D Histogram.
"""
histogram = hist.Hist(tree.to_hist())
histogram.plot()
plt.title(uproot_browser.plot.make_hist_title(tree, histogram))