Skip to content

Instantly share code, notes, and snippets.

@bigdot123456
Forked from GeorgeSeif/overlaid_histogram.py
Created April 29, 2018 00:26
Show Gist options
  • Select an option

  • Save bigdot123456/a8d26ff5c445155c561e774385189ecd to your computer and use it in GitHub Desktop.

Select an option

Save bigdot123456/a8d26ff5c445155c561e774385189ecd to your computer and use it in GitHub Desktop.

Revisions

  1. @GeorgeSeif GeorgeSeif created this gist Mar 1, 2018.
    21 changes: 21 additions & 0 deletions overlaid_histogram.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # 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')