0

I tried to plot a distribution pdf and cdf in one plot. If plot together, pdf and cdf are not matched. If plot separately, they will match. Why? You can see both green curves from same equation, but shows different shape...

 def MBdist(n,loct,scale):
        data = maxwell.rvs(loc=loct, scale=scale, size=n)
        params = maxwell.fit(data, floc=0)
        return data, params



if __name__ == '__main__':
    data,para=MBdist(10000,0,0.5)
    plt.subplot(211)
    plt.hist(data, bins=20, normed=True)
    x = np.linspace(0, 5, 20)
    print x

    plt.plot(x, maxwell.pdf(x, *para),'r',maxwell.cdf(x, *para), 'g')
    plt.subplot(212)
    plt.plot(x, maxwell.cdf(x, *para), 'g')
    plt.show()

Both green curves from same equation, but get different results.

rfeynman
  • 97
  • 1
  • 9
  • 2
    You also don't pass in an 'x' to go with the second line so it is plotting against index. – tacaswell Feb 21 '15 at 00:42
  • @tcaswell: You should probably just add your comment as an answer (esp since it appears to be the correct one). – Gerrat Feb 21 '15 at 12:59

1 Answers1

3

You also don't pass in an 'x' to go with the second line so it is plotting against index. It should be

plt.plot(x, maxwell.pdf(x, *para),'r',x, maxwell.cdf(x, *para), 'g')

This interface is a particularly magical bit of arg-parsing that was mimicked from MATLAB. I would suggest

fig, ax = plt.subplots()
ax.plot(x, maxwell.pdf(x, *para),'r')
ax.plot(x, maxwell.cdf(x, *para), 'g')

which while a bit more verbose line-wise is much clearer.

tacaswell
  • 84,579
  • 22
  • 210
  • 199