Matplotlib中的bin大小(直方图) [英] Bin size in Matplotlib (Histogram)

查看:304
本文介绍了Matplotlib中的bin大小(直方图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib制作直方图.

I'm using matplotlib to make a histogram.

有什么方法可以手动设置垃圾箱的大小,而不是垃圾箱的数量吗?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆