gpt4 book ai didi

python - Pyplot - 可视化列表的直方图

转载 作者:行者123 更新时间:2023-12-01 08:39:42 32 4
gpt4 key购买 nike

给定一个简单的负值列表:l = [0, -1, -1, -1, -10, -100]

使用标准方法 plt.hist(l) 可视化直方图的最快方法是什么?

我希望能够查看列表中的所有条目及其相对频率。

正在做:

from matplotlib import pyplot as plt 
l = [0, -1, -1, -1, -10, -100]
plt.hist(l)
plt.show()

结果:

enter image description here

在当前情况下设置垃圾箱数量的正确方法是什么?

非常感谢任何帮助

最佳答案

假设列表中的所有条目l都是整数。您可以使用bins控制直方图中箱的数量或大小。

from matplotlib import pyplot as plt
l = [0, -1, -1, -1, -10, -100]
plt.hist(l, bins=max(l)-min(l)+1)
ax = plt.subplot()
ax.set_xticks(l)
plt.show()

enter image description here

关于python - Pyplot - 可视化列表的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53563492/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com