1D histograms#
The examples below assume the following imports:
import atompy as ap
import matplotlib.pyplot as plt
import numpy as np
First, create a Hist1d object.
You can load a histogram from a text file (load_1d_from_txt()),
a ROOT file (load_1d_from_root()), or you
instantiate it with the output from numpy.histogram().
# load from text file
hist = ap.load_1d_from_txt("path/to/file.txt", output="Hist1d")
# load from ROOT file
hist = ap.load_1d_from_root("path/to/file.root", output="Hist1d")
# instantiate from np.histogram output
hist = ap.Hist1d(*np.histogram(some_data))
After you loaded a histogram, you can do a number of operations on it
(for a full list, see here).
Reformat for plot and step#
A histogram is stored as bin-edges and histogram-values, where the edges array is one larger than the values array. When plotting a histogram, this has to be considered (otherwise, the plotted data may be shifted)
Hist1d.for_plot() and Hist1d.for_step() are two convenience
properties of Hist1d that return two arrays which can be used to
plot the histogram using matplotlib.pyplot.plot and
matplotlib.pyplot.step, respectively.
Attention
matplotlib.pyplot.step shifts the plot corresponding to its
where keyword. Hist1d.for_step() assumes the default behavior
of where = pre. Any other keyword will result in incorrectly shifted
bins.
The following code snippet showcases the usage:
x, y = hist.for_plot
plt.plot(x, y)
x, y = hist.for_step
plt.step(x, y)
With this functionality, one can do the above in one line of code by directly
unpacking the returns using the * operator:
# unpack directly in the function call
plt.plot(*hist.for_plot)
plt.step(*hist.for_step)
Chain multiple method calls#
Some methods of Hist1d return a new instance of Hist1d.
Then, one can chain multiple operations in one line of code.
E.g., first, one can rebin the histogram, then normalize it to it’s maximum, discard anything that is not within a specific range, then plot it; all in one line:
plt.plot(hist.rebinned(2).normalized_to_integral.within_range(0, 10).for_plot)