#!/usr/bin/env python # coding: utf-8 # # Merging bins # In[1]: from physt.binnings import * from physt import h1, h2 import numpy as np np.random.seed(42) # In[2]: data = np.random.rand(100) # In[3]: hh = h1(data, 120) hh.plot(errors=True); # In[4]: hh.merge_bins(2, inplace=True) hh.plot(errors=True); # In[5]: hh.merge_bins(2, inplace=True) hh.plot(errors=True); # In[6]: hh.merge_bins(2, inplace=True) hh.plot(errors=True); # In[7]: hh.merge_bins(2, inplace=True) hh.plot(errors=True); # In[8]: hh.merge_bins(2, inplace=True) hh.plot(errors=True); # In[9]: hh.merge_bins(2, inplace=True) hh.plot(errors=True); # ### By min frequency # # In[10]: data = np.random.normal(0, 1, 5000) hh = h1(data, 120) hh.plot(); # In[11]: hh.merge_bins(min_frequency=100, inplace=True) hh.plot(density=True); # In[12]: 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. # In[13]: # 2D example data1 = np.random.normal(0, 1, 600) data2 = np.random.rand(600) # In[14]: 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");