Counting occurences after bin fitting of values

import numpy as np from bisect import bisect import matplotlib.pyplot as plt def count_binned(matrix, intervals): bins = np.array([bisect(intervals, v) - 1 for v in matrix.ravel()]) return np.bincount(bins) range_from, range_to, bins, values = -100, 100, 8, 100 bin_step = (range_to - range_from) / (bins+1) intervals = [range_from + k*bin_step for k in range(int(bin_step)) if range_from + k * bin_step < range_to] np.random.seed(1) A = np.random.randint(range_from, range_to, values) bin_counts = count_binned(A, intervals) plt.title('Counting occurences after bin fitting of values') for i, bc in enumerate(bin_counts): plt.bar(i, bc, width=0.9, edgecolor='white', color=plt.cm.Vega20(i), lw=6, alpha=0.5) plt.xticks(range(len(bin_counts))) plt.xlabel('bins') plt.ylabel('occurences') plt.tight_layout() plt.show() Counting occurences after bin fitting of values