If you do not set the bins
parameter yourself, plt.hist
will choose (by default, 10) bins for you:
In [58]: n, bins, patches = plt.hist(X, normed=False, histtype='step', cumulative=True)
In [59]: bins
Out[59]:
array([ 1.1 , 1.38, 1.66, 1.94, 2.22, 2.5 , 2.78, 3.06, 3.34,
3.62, 3.9 ])
The return value bins
shows the edges of the bins that matplotlib chose.
It sounds like you want the values in X to serve as bin edges. Using
bins=sorted(X)+[np.inf]
:
import numpy as np
import matplotlib.pyplot as plt
X = [1.1, 3.1, 2.1, 3.9]
bins = sorted(X) + [np.inf]
n, bins, patches = plt.hist(X, normed=False, histtype='step', cumulative=True,
bins=bins)
plt.ylim([0, 5])
plt.grid()
plt.show()
yields
The [np.inf]
makes the right edge of the final bin extend to infinity. Matplotlib is smart enough to not try to draw non-finite values, so all you see is the left-edge of the last bin.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…