xhistogram
Fast, flexible, label-aware histograms for numpy and xarray
https://xhistogram.readthedocs.io/en/latest/
https://github.com/xgcm/xhistogram
Histograms (a.k.a “binning”) are mu.ch more than just a visualization tool. They are the foundation of a wide range of scientific analyses including joint probability distributions and coordinate transformations. Xhistogram makes it easier to calculate flexible, complex histograms with multi-dimensional data. It integrates (optionally) with Dask, in order to scale up to very large datasets and with Xarray, in order to consume and produce labelled, annotated data structures. It is useful for a wide range of scientific tasks.
Example
Area- (or volume-) weighted T–S PDF
code:python
import numpy as np
import xarray as xr
from xhistogram.xarray import histogram
T = ds"thetao"
S = ds"so"
# Example: horizontal area weights (for volume weighting, use area * dz, etc.)
w = ds"cell_area"
# 2-D T–S histogram / PDF (example bin edges)
pdf = histogram(
T, S,
bins=[
xr.DataArray(np.linspace(-2, 30, 129), dims="binT"),
xr.DataArray(np.linspace(30, 40, 129), dims="binS"),
],
weights=w,
dim="x", "y", # reduce over x,y to form the distribution (you can keep time/z)
)