Special histograms in physt

Sometimes, it is necessary to bin values in transformed coordinates (e.g. polar). In principle, it is possible to create histograms from already transformed values (i.e. r and φ). However, this is not always the best way to go as each set of coordinates has its own peculiarities (e.g. the typical range of values for azimuthal angle)

Physt provides a general framework for constructing the transformed histograms (see a dedicated section of this document) and a couple of most frequently used variants:

  • PolarHistogram
  • SphericalHistogram
  • CylindricalHistogram
# Necessary import evil
%matplotlib inline
from physt import histogram, binnings, special
import numpy as np
import matplotlib.pyplot as plt
# Generate some points in the Cartesian coordinates

x = np.random.rand(1000)
y = np.random.rand(1000)
z = np.random.rand(1000)

Polar histogram

This histograms maps values to radius (r) and azimuthal angle (φ, ranging from 0 to 2π).

By default (unless you specify the phi_bins parameter), the whole azimuthal range is spanned (even if there are no values that fall in parts of the circle).

# Create a polar histogram with default parameters
hist = special.polar_histogram(x, y)
ax = hist.plot.polar_map()
PolarHistogram(bins=(10, 16), total=1000, dtype=int64)
# Create a polar histogram with different binning
hist2 = special.polar_histogram(x+.3, y+.3, radial_bins="human", phi_bins="human")
ax = hist2.plot.polar_map(density=True)
# Default axes names
('r', 'phi')

When working with any transformed histograms, you can fill values in the original, or transformed coordinates. All methods working with coordinates understand the parameter transformed which (if True) says that the method parameter are already in the transformed coordinated; otherwise, all values are considered to be in the original coordinates and transformed on inserting (creating, searching).

# Using transformed / untransformed values
print("Non-transformed", hist.find_bin((0.1, 1)))
print("Transformed", hist.find_bin((0.1, 1), transformed=True))

print("Non-transformed", hist.find_bin((0.1, 2.7)))     # Value
print("Transformed", hist.find_bin((0.1, 2.7), transformed=True))
Non-transformed (7, 3)
Transformed (0, 2)
Non-transformed None
Transformed (0, 6)
# Simple plotting, similar to Histogram2D
hist.plot.polar_map(density=True, show_zero=False, cmap="Wistia", lw=0.5, figsize=(5, 5));

Adding new values

# Add a single, untransformed value
hist.fill((-.5, -.5), weight=12)
hist.plot.polar_map(density=True, show_zero=True, cmap="Reds", lw=0.5, figsize=(5, 5));
# Add a couple of values, transformed
data = [[.5, 3.05], [.5, 3.2], [.7, 3.3]]
weights = [1, 5, 20]

hist.fill_n(data, weights=weights, transformed=True)
hist.plot.polar_map(density=True, show_zero=True, cmap="Reds", lw=0.5, figsize=(5, 5));


The projections are stored using specialized Histogram1D subclasses that keep (in the case of radial) information about the proper bin sizes.

radial = hist.projection("r")
radial.plot(density=True, color="red", alpha=0.5).set_title("Density")
radial.plot(label="absolute", color="blue", alpha=0.5).set_title("Absolute")
radial.plot(label="cumulative", cumulative=True, density=True, color="green", alpha=0.5).set_title("Cumulative")
RadialHistogram(bins=(10,), total=1038, dtype=int64)
<matplotlib.axes._subplots.AxesSubplot at 0x7fbf5406e518>

Cylindrical histogram

To be implemented

data = np.random.rand(100, 3)
h = special.cylindrical_histogram(data)
CylindricalHistogram(bins=(10, 16, 10), total=100, dtype=int64)
# %matplotlib qt
proj = h.projection("rho", "phi")
PolarHistogram(bins=(10, 16), total=100, dtype=int64)
proj = h.projection("phi", "z")
ax = proj.plot.cylinder_map()
ax.view_init(50, 70)
CylinderSurfaceHistogram(bins=(16, 10), total=100, dtype=int64)

Spherical histogram

To be implemented

n = 1000000
data = np.empty((n, 3))
data[:,0] = np.random.normal(0, 1, n)
data[:,1] = np.random.normal(0, 1.3, n)
data[:,2] = np.random.normal(1, 1.2, n)
h = special.spherical_histogram(data)
SphericalHistogram(bins=(10, 16, 16), total=1000000, dtype=int64)
globe = h.projection("theta", "phi")
# globe.plot()
globe.plot.globe_map(density=True, figsize=(7, 7), cmap="rainbow")
globe.plot.globe_map(density=False, figsize=(7, 7))
DirectionalHistogram(bins=(16, 16), total=1000000, dtype=int64)

Implementing custom transformed histogram