【问题标题】:Histograms in PandasPandas 中的直方图
【发布时间】:2016-09-22 00:02:59
【问题描述】:

python 和 pandas 相对较新。我有一个数据框:df 有 2 列(比如,01)和 n 行。我想绘制两列中表示的两个时间序列数据的直方图。我还需要访问直方图中每个 bin 的准确计数,以便以后进行操作。

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),这是解决此问题的最佳/最简单方法吗?

【问题讨论】:

  • 你能发布一个Minimal, Complete and Verifiable 的例子吗?您正在调用来自 npplt 的函数,但不清楚它们是什么或您的输入数据是什么样的。

标签: python pandas numpy histogram bins


【解决方案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 直方图的值。见密度和重量 对可能语义的描述。

bin_edges : dtype 数组 float 返回 bin 边缘 (length(hist)+1)

很清楚,不是吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-21
    • 2015-10-12
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2017-03-11
    相关资源
    最近更新 更多