Union find
Union find

Reputation: 8160

CDF/PDF plot not showing

I am able to get a histogram from a Pandas dataframe to appear fine. I'd like to also show the PDf/CDF as line charts on the same plot.

My code:

import scipy.stats as stats
from scipy.stats import norm

samples=twentyandmiddle['age']
print samples.head(5)

plt.hist(samples, bins=40, normed=True)

#samples=samples[0]
plt.plot(samples,norm.pdf(samples), 'r-', lw=5, alpha=0.6, label='sample pdf')
plt.plot(samples,norm.cdf(samples), 'k-', lw=2, alpha=0.6, label='sample cdf')
plt.legend(loc='best', frameon=False)
plt.show()

My issue is: The cdf/pdf lines are just 0 across the graph, like this:

cdf/pdf

What's wrong?

The head outputs this:

0    59
1    58
2    57
3    54
4    51
Name: age, dtype: int64

Upvotes: 0

Views: 457

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114946

norm.pdf(x) computes the PDF of the standard normal disttribution, with mean 0 and std. dev. 1. It looks like your values are all larger than 20. norm.pdf(20) is 5.52e-88. Similarly, norm.cdf(20) is 1.0.

Did you mean to first fit a normal distribution to your data, and plot the PDF and CDF of the fitted distribution? If so, you'll need to include the fitted parameters in the calls; e.g. norm.pdf(x, loc=mu, scale=stddev) and norm.cdf(x, loc=mu, scale=stddev).

Upvotes: 1

Related Questions