Search code examples
matplotliblinehistogramcurve

Matplotlib: plotting curves with histogram data


Currently have 3 sets of data in a single histogram, but I need to plot a single curve for each set of data. Currently plotting histograms through a function:

def plot_histogram(xmin,xmax,title,xlabel,data,data1,data2):
    plt.xlim(xmin, xmax)
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel('Relative Frequency')
    bins=np.histogram(np.hstack((data,data1,data2)), bins=11)[1]
    plt.hist([data, data1, data2], bins, normed=1, alpha=0.5, color=['b', 'g', 'r'], label=['t', 'g-', 'g+'])
    plt.legend(loc='upper right')
    plt.show()

How can I plot a curve or line which follows the shape of each histogram?


Solution

  • Using a kde did give me the plot I wanted, but in fact I would suggest using seaborn for this, as the module has a command distplot which plots a histogram and density plot at the same time.

    Examples of using seaborn given here: http://nbviewer.ipython.org/github/mwaskom/seaborn/blob/master/examples/plotting_distributions.ipynb