# Overlay 2 histograms to compare them def overlaid_histogram(data1, data2, n_bins = 0, data1_name="", data1_color="#539caf", data2_name="", data2_color="#7663b0", x_label="", y_label="", title=""): # Set the bounds for the bins so that the two distributions are fairly compared max_nbins = 10 data_range = [min(min(data1), min(data2)), max(max(data1), max(data2))] binwidth = (data_range[1] - data_range[0]) / max_nbins if n_bins == 0 bins = np.arange(data_range[0], data_range[1] + binwidth, binwidth) else: bins = n_bins # Create the plot _, ax = plt.subplots() ax.hist(data1, bins = bins, color = data1_color, alpha = 1, label = data1_name) ax.hist(data2, bins = bins, color = data2_color, alpha = 0.75, label = data2_name) ax.set_ylabel(y_label) ax.set_xlabel(x_label) ax.set_title(title) ax.legend(loc = 'best')