Plotting a Continuous Probability Distribution Function(PDF) from a Histogram – Solved in Python

Assuming you have an array of number to which you need to plot a histogram. Plot the histogram in python using the following code.

             n, bins, patches = plt.hist(D, 40, histtype='bar')
         plt.show()

The resulting plot looks like image 1. Once you have histogram you know need transform the histogram such that sum of frequency of all the points is 1
Image 1

To do that just use the following code in python. We are just diving each range by total number of data points.

         n = n/len(A)
         n = np.append(n, 0)

Now we have histogram whose sum of probability of all the points of occurrence is 1.

The final Step:
If your histogram looks like a Gaussian distribution, plot the mean and variance for the histogram.

         mu = np.mean(n)
         sigma = np.std(n)
 
Once you have the mean and variance plot the gaussian pdf function. There might be problem in scale and you might get peak at a higher value. To fix this just scale this to a factor it fits. A final image in shown in image 2.

         mu = np.mean(n)
         sigma = np.std(n)
         plt.bar(bins,n, width=(bins[len(bins)-1]-bins[0])/40)
         y1= (1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins - mu)**2 /(2*sigma**2)))*0.03
         plt.plot(bins, y1, 'r--', linewidth=2)
Image 2

Finally we have the probability distribution function as 
1/(sigma*np.sqrt(2*np.pi))*np.exp(-(bins - mu)**2 /(2*sigma**2)))*0.03

PS : Imports
import numpy as np
import matplotlib
from matplotlib import pyplot as plt