matplotlib直方图函数中箱子信息的获取

2024-09-28 20:49:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: 数据信息元素数量matplotlib绘制plt直方图
1条回答
网友
1楼 · 发布于 2024-09-28 20:49:52

^{} 的返回值是:

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

所以您只需要适当地捕获返回值。例如:

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的边缘。

相关问题 更多 >