Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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')
@scipliccodes
Copy link

scipliccodes commented Apr 4, 2018

Hi,
it seams there is a problem with the code in line 9. I gues there is a missing colon at the end of the line:

if n_bins == 0:
instead of
if n_bins == 0

Best regards,
Robin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment