Matplotlib 中的 bin 大小(直方图) [英] Bin size in Matplotlib (Histogram)
本文介绍了Matplotlib 中的 bin 大小(直方图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 matplotlib 制作直方图.
I'm using matplotlib to make a histogram.
有没有办法手动设置 bin 的大小而不是 bin 的数量?
Is there any way to manually set the size of the bins as opposed to the number of bins?
推荐答案
实际上,这很容易:您可以给出一个包含 bin 边界的列表,而不是 bin 数量.它们也可能分布不均:
Actually, it's quite easy: instead of the number of bins you can give a list with the bin boundaries. They can be unequally distributed, too:
plt.hist(data, bins=[0, 10, 20, 30, 40, 50, 100])
如果你只是想让它们平均分布,你可以简单地使用范围:
If you just want them equally distributed, you can simply use range:
plt.hist(data, bins=range(min(data), max(data) + binwidth, binwidth))
<小时>
添加到原始答案
以上行仅适用于填充整数的 data
.正如 macrocosme 指出的那样,对于浮动,您可以使用:
The above line works for data
filled with integers only. As macrocosme points out, for floats you can use:
import numpy as np
plt.hist(data, bins=np.arange(min(data), max(data) + binwidth, binwidth))
这篇关于Matplotlib 中的 bin 大小(直方图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文