您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Matplotlib累积直方图-垂直线放置错误或误解?

Matplotlib累积直方图-垂直线放置错误或误解?

如果不设置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 ])

返回值bins显示matplotlib选择的垃圾箱的边缘。

听起来您想让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的左边缘。

其他 2022/1/1 18:42:55 有456人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶