对于 python 和 pandas 来说相对较新。我有一个数据框:df
,其中有 2 列(例如 0
和 1
)和 n 行。我想绘制两列中表示的两个时间序列数据的直方图。我还需要访问每个箱的直方图中的确切计数,以便以后进行操作。
b_counts, b_bins = np.histogram(df[0], bins = 10)
a_counts, a_bins = np.histogram(df[1], bins = 10)
plt.bar(b_bins, b_counts)
plt.pbar(a_bins, a_counts)
但是,我收到大小不兼容的错误,即 bins 数组的长度为 11,而 counts 数组的长度为 10。两个问题: 1)为什么numpy中的直方图有一个额外的bin?即 11 个垃圾箱而不是 10 个垃圾箱 2)假设上面的问题1)可以解决,这是解决这个问题的最好/最简单的方法吗?
最佳答案
我会直接使用 Pyplot 内置的 histogram功能:
b_counts, b_bins, _ = plt.hist(df[0], bins = 10)
a_counts, a_bins, _ = plt.hist(df[1], bins = 10)
<小时/>
根据 numpy.histogram 的文档(如果您向下滚动足够远以阅读参数定义中的 Returns
部分):
hist : array The values of the histogram. See density and weights for a description of the possible semantics.
bin_edges : array of dtype float Return the bin edges
(length(hist)+1)
.
很清楚,不是吗?
关于python - Pandas 中的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39628242/