def h1n(n, nx, ny, names, h1ds, bins, ranges, xlabels, ylabels, titles=None, legends=None, figsize=(10, 10)): fig = plt.figure(figsize=figsize) stats = {} for i in range(n): ax = fig.add_subplot(nx, ny, i + 1) x = h1ds[i] r = ranges[i] x1 = loc_elem_1d(x, find_nearest(x, r[0])) x2 = loc_elem_1d(x, find_nearest(x, r[1])) xmin = min(x1, x2) xmax = max(x1, x2) x2 = x[xmin:xmax] o = np.ones(len(x2)) mu, std = weighted_mean_and_std(x2, o) stats[names[i]] = Measurement(mu, std) ax.set_xlabel(xlabels[i], fontsize=11) ax.set_ylabel(ylabels[i], fontsize=11) ax.hist(x, bins=bins[i], range=r, histtype='step', edgecolor='black', linewidth=1.5, label=r'$\mu={:7.2f},\ \sigma={:7.2f}$'.format(mu, std)) ax.legend(fontsize=10, loc=legends[i]) plt.grid(True) if titles: plt.title(titles[i]) plt.tight_layout() return stats
def h1d(x, bins=None, range=None, weights=None, xlabel='Variable', ylabel='Frequency', title=None, legend='upper right', figsize=(6, 6)): fig = plt.figure(figsize=figsize) ax = fig.add_subplot(1, 1, 1) x1 = loc_elem_1d(x, find_nearest(x, range[0])) x2 = loc_elem_1d(x, find_nearest(x, range[1])) xmin = min(x1, x2) xmax = max(x1, x2) mu, std = weighted_mean_and_std(x[xmin:xmax], np.ones(len(x[xmin:xmax]))) ax.set_xlabel(xlabel, fontsize=11) ax.set_ylabel(ylabel, fontsize=11) ax.hist(x, bins=bins, range=range, weights=weights, histtype='step', edgecolor='black', linewidth=1.5, label=r'$\mu={:7.2f},\ \sigma={:7.2f}$'.format(mu, std)) ax.legend(fontsize=10, loc=legend) plt.grid(True) if title: plt.title(title) return mu, std
def bin_to_last_ratio(array, bins, xbin): return np.sum(array[loc_elem_1d(bins, xbin):-1]) / np.sum(array)
def bin_ratio(array, bins, xbin): return array[loc_elem_1d(bins, xbin)] / np.sum(array)