1

I need to draw the density curve on the Histogram with the actual height of the bars (actual frequency) as the y-axis.

Try1: I found a related answer here but, it has normalized the Histogram to the range of the curve.

Below is my code and the output.

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from scipy.stats import norm

data = [125.36, 126.66, 130.28, 133.74, 126.92, 120.85, 119.42, 128.61, 123.53, 130.15, 126.02, 116.65, 125.24, 126.84,
        125.95, 114.41, 138.62, 127.4, 127.59, 123.57, 133.76, 124.6, 113.48, 128.6, 121.04, 119.42, 120.83, 136.53, 120.4,
        136.58, 121.73, 132.72, 109.25, 125.42, 117.67, 124.01, 118.74, 128.99, 131.11, 112.27, 118.76, 119.15, 122.42,
        122.22, 134.71, 126.22, 130.33, 120.52, 126.88, 117.4]
(mu, sigma) = norm.fit(data)
x = np.linspace(min(data), max(data), 100)

plt.hist(data, bins=12, normed=True)
plt.plot(x, mlab.normpdf(x, mu, sigma))

plt.show()

Result when normed is True Try2: There @DavidG has given an option, a user defined function even it doesn't cover the density of the Histogram accurately.

def gauss_function(x, a, x0, sigma):
    return a * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))
test = gauss_function(x, max(data), mu, sigma)

plt.hist(data, bins=12)
plt.plot(x, test)
plt.show() 

The result for this was, enter image description here But the actual Histogram is below, where Y-axis ranges from 0 to 8, enter image description here And I want to draw the density curve exactly on that. Any help this regards will be really appreciated.

1 Answers1

3

Is this what you're looking for? I'm multiplying the pdf by the area of the histogram.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

data = [125.36, 126.66, 130.28, 133.74, 126.92, 120.85, 119.42, 128.61, 123.53, 130.15, 126.02, 116.65, 125.24, 126.84,
        125.95, 114.41, 138.62, 127.4, 127.59, 123.57, 133.76, 124.6, 113.48, 128.6, 121.04, 119.42, 120.83, 136.53, 120.4,
        136.58, 121.73, 132.72, 109.25, 125.42, 117.67, 124.01, 118.74, 128.99, 131.11, 112.27, 118.76, 119.15, 122.42,
        122.22, 134.71, 126.22, 130.33, 120.52, 126.88, 117.4]

(mu, sigma) = norm.fit(data)
x = np.linspace(min(data), max(data), 100)

values, bins, _ = plt.hist(data, bins=12)
area = sum(np.diff(bins) * values)

plt.plot(x, norm.pdf(x, mu, sigma) * area, 'r')
plt.show()

Result:

enter image description here

Scratch'N'Purr
  • 9,959
  • 2
  • 35
  • 51