在 matplotlib 直方图函数中获取 bin 的信息 [英] Getting information for bins in matplotlib histogram function

查看:83
本文介绍了在 matplotlib 直方图函数中获取 bin 的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 matplotlib 在 python 中绘制直方图:

I am plotting a histogram in python using matplotlib by:

plt.hist(nparray, bins=10, label='hist')

是否可以打印一个包含所有 bin 信息的数据帧,例如每个 bin 中的元素数量?

Is it possible to print a dataframe that has the information for all the bins, like number of elements in every bin?

推荐答案

plt.hist 是:

返回值:元组:(n个,bins,patch)或([[n0,n1 ... ...],bins,[patches0,patch1,...])

Returns: tuple : (n, bins, patches) or ([n0, n1, ...], bins, [patches0, patches1,...])

所以您需要做的就是适当地捕获返回值.例如:

So all you need to do is capture the return values appropriately. For example:

import numpy as np
import matplotlib.pyplot as plt

# generate some uniformly distributed data
x = np.random.rand(1000)

# create the histogram
(n, bins, patches) = plt.hist(x, bins=10, label='hst')

plt.show()

# inspect the counts in each bin
In [4]: print n
[102  87 102  83 106 100 104 110 102 104]

# and we see that the bins are approximately uniformly filled.
# create a second histogram with more bins (but same input data)
(n2, bins2, patches) = plt.hist(x, bins=20, label='hst')

In [34]: print n2
[54 48 39 48 51 51 37 46 49 57 50 50 52 52 59 51 58 44 58 46]

# bins are uniformly filled but obviously with fewer in each bin.

返回的 bins 定义了所使用的每个bin的边缘.

The bins that is returned defines the edges of each bin that was used.

这篇关于在 matplotlib 直方图函数中获取 bin 的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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