from physt.binnings import *
from physt import h1, h2
import numpy as np
np.random.seed(42)
data = np.random.rand(100)
hh = h1(data, 120)
hh.plot(errors=True);
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
hh.merge_bins(2, inplace=True)
hh.plot(errors=True);
data = np.random.normal(0, 1, 5000)
hh = h1(data, 120)
hh.plot();
hh.merge_bins(min_frequency=100, inplace=True)
hh.plot(density=True);
hh.merge_bins(min_frequency=600, inplace=True)
hh.plot(density=True);
The same can be done for 2D histograms (i.e. each column, each row should contain more than the minimum). Unfortunately, a general, irregular-shaped binning is not yet supported.
# 2D example
data1 = np.random.normal(0, 1, 600)
data2 = np.random.rand(600)
hh = h2(data1, data2, 23)
ax = hh.plot(show_zero=0, cmap="rainbow", show_colorbar=False);
ax.set_title("Before merging")
hh.merge_bins(min_frequency=30, inplace=True)
ax = hh.plot(density=True, show_zero=False, cmap="rainbow", show_colorbar=False)
ax.set_title("After merging");