from_txt#

classmethod Hist2d.from_txt(fname, *, iteration_order='x_first', data_layout='columns', xyz_indices=(0, 1, 2), xmin=None, xmax=None, ymin=None, ymax=None, title='', xlabel='', ylabel='', zlabel='', **loadtxt_kwargs)[source]#

Initiate a Hist2d from a text file.

Parameters:
fnamestr | PathLike

The path to the text file.

iteration_order{“x_first”, “y_first”}, default: “x_first”

The order in which the data iterates through the x and y dimensions.

data_layout{“rows”, “columns”}, default: “columns”

The layout of the data in the file, either row-major or column-major.

xyz_indicestuple[int, int, int], default: (0, 1, 2)

A tuple specifying the column (or row, depending on data_layout) indices for x, y, and z (values) data.

xmin/xmax/ymin/ymaxfloat, optional

The lower (upper) x (y) bound to use when converting centers to edges. Either the lower or the upper bound must be provided if the bins have unequal size. Unnecessary for equally sized bins.

**loadtxt_kwargs

Additional keyword arguments to pass to numpy.loadtxt().

Returns:
Hist2d

A new Hist2d instance.

Examples

Given a file named data.txt with the following content:

#x  y  z
0.5  10.0  1
1.5  10.0  3
2.5  10.0  5
0.5  20.0  2
1.5  20.0  4
2.5  20.0  6
>>> # Create a dummy data.txt file for demonstration
>>> with open("data.txt", "w") as f:
...     f.write("#x  y  z\n")
...     f.write("0.5  10.0  1\n")
...     f.write("1.5  10.0  3\n")
...     f.write("2.5  10.0  5\n")
...     f.write("0.5  20.0  2\n")
...     f.write("1.5  20.0  4\n")
...     f.write("2.5  20.0  6\n")
>>> hist = Hist2d.from_txt("data.txt")
>>> print(hist.xedges)
[0., 1., 2., 3.]
>>> print(hist.yedges)
[5., 15., 25.]
>>> print(hist.values)
[[1, 2], [3, 4], [5, 6]]