如果不设置该bins
参数的自己,plt.hist
会为你选择(默认情况下,10)箱:
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 ])
听起来您想让X中的值充当bin边缘。使用 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()
产量
在[np.inf]
做出最终斌的右边缘延伸到无穷大。Matplotlib很聪明,不会尝试绘制非有限值,因此您所看到的只是最后一个bin的左边缘。