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:
In [1]:
# Necessary import evil
%matplotlib inline
#
from physt import histogram, binnings, special
import numpy as np
import matplotlib.pyplot as plt
In [2]:
# Generate some points in the Cartesian coordinates
np.random.seed(42)
x = np.random.rand(1000)
y = np.random.rand(1000)
z = np.random.rand(1000)
In [3]:
# Create a polar histogram with default parameters
hist = special.polar_histogram(x, y)
ax = hist.plot.polar_map()
hist
Out[3]:
In [4]:
hist.bins
Out[4]:
In [5]:
# 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)
In [6]:
# Default axes names
hist.axis_names
Out[6]:
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).
In [7]:
# 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))
In [8]:
# Simple plotting, similar to Histogram2D
hist.plot.polar_map(density=True, show_zero=False, cmap="Wistia", lw=0.5, figsize=(5, 5));
In [9]:
# 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));
In [10]:
# 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));
In [11]:
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")
radial
Out[11]:
In [12]:
hist.projection("phi").plot(cmap="rainbow")
Out[12]:
In [13]:
data = np.random.rand(100, 3)
h = special.cylindrical_histogram(data)
h
Out[13]:
In [14]:
# %matplotlib qt
proj = h.projection("rho", "phi")
proj.plot.polar_map()
proj
Out[14]:
In [15]:
proj = h.projection("phi", "z")
ax = proj.plot.cylinder_map(show_zero=False)
ax.view_init(50, 70)
proj
Out[15]:
In [16]:
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)
h
Out[16]:
In [17]:
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))
globe
Out[17]: