2D histograms#

The examples below assume the following imports:

import atompy as ap
import matplotlib.pyplot as plt
import numpy as np

First, create a Hist2d object.

You can load a histogram from a text file (load_2d_from_txt()), a ROOT file (load_2d_from_root()), or you instantiate it with the output from numpy.histogram2d().

# load from text file
hist = ap.load_2d_from_txt("path/to/file.txt", output_format="Hist2d")

# load from ROOT file
hist = ap.load_2d_from_root("path/to/file.root", output_format="Hist2d")

# instantiate from np.histogram output
hist = ap.Hist2d(*np.histogram(some_xdata, some_ydata))

After you loaded a histogram, you can do a number of operations on it (for a full list, see here).

Plot histograms using pcolormesh or imshow#

To plot a histogram, you can use matplotlib.pyplot.pcolormesh or matplotlib.pyplot.imshow.

Tip

When using matplotlib.pyplot.pcolormesh, set the keyword rasterized = True. This can drastically reduce the resulting file size.

For convenience, Hist2d has two properties Hist2d.for_pcolormesh and Hist2d.for_imshow, the result in appropriate output that can be used for plotting.

Basic usage#

x, y, z = hist.for_pcolormesh
plt.pcolormesh(x, y, z, rasterized=True)

image, extent = hist.for_imshow
plt.imshow(image, extent=extent)

Hist2d.for_pcolormesh and Hist2d.for_imshow don’t simply return three (or two) arrays, but wrap those in a special class, namely PcolormeshData and ImshowData, respectively.

This way, the above lines can be compressed to one-liners each

plt.pcolormesh(**hist.for_pcolormesh())

plt.imshow(**hist.for_imshow())

For details on the possible syntax, see the documentation pages of PcolormeshData and ImshowData.

Change colormap#

There are multiple ways to change the colormap that is used for 2D plots in matplotlib. My favorite one is to configure rcParams to use the desired colormap.

plt.rcParams["image.cmap"] = "atom"

With the above method, one can use any _registered_ colormap. When loading atompy, an “atomic physics” colormap is registered, which then is used by the above method.

Instead, one could also use a keyword argument in the plotting call:

plt.imshow(**hist.for_imshow(), cmap="atom")

In these examples, zeros are included in the colormap. If this is unwanted, one can conveniently remove them with the Hist2d.without_zeros method (see also here):

plt.imshow(**hist.without_zeros.for_imshow())

Common histogram operations#

For an exhaustive overview of the provided methods, see the documentation pages of Hist2d.

Projections#

See also here (x) and here (y).

Methods Hist2d.projected_onto_x and Hist2d.projected_onto_y project the 2D histogram onto the _x_ and _y_ axis, respectively, returning a Hist1d object.

For example

hist1d = hist2d.projected_onto_x
plt.step(*hist1d.for_step)

If one wants to only project the histogram within a specific region, one can first apply a gate, then project. For example

hist1d = hist2d.within_yrange(ymin, ymax).projected_onto_y
plt.step(*hist1d.for_step)

Applying gates#

As seen in the above example, one can apply gates using Hist2d.within_xrange or Hist2d.within_yrange.

For example

gated_hist = hist2d.within_xrange(xmin, xmax).within_yrange(ymin.ymax)

All examples#