【问题标题】:Call upon individual bins in histograms in Matplotlib?在 Matplotlib 中调用直方图中的各个 bin?
【发布时间】:2020-03-12 15:01:39
【问题描述】:

我需要使用三个直方图的每个单独的 bin 来在方程式中使用(即来自 histogram1、histogram2 和 histogram3 的 bin1,然后来自 h1、h2、h3 等的 bin2)。

有没有办法从直方图中调用各个 bin 来使用?

提前致谢

【问题讨论】:

  • 你能提供一个看起来像你想做的示例图表吗?
  • 欢迎堆栈溢出。请提供minimal working example,以便人们开始理解您的问题。

标签: python matplotlib histogram


【解决方案1】:

是的,当您调用 plt.hist 时,将返回 bin 的位置以及每个 bin 中的条目数。假设您生成了三个直方图(我将使用直方图 0、1 和 2,因为 python):

import matplotlib.pyplot as plt
import numpy as np

x0 = np.random.rand(25)
x1 = np.random.rand(25)
x2 = np.random.rand(25)

counts0, bins0, patches0 = plt.hist(x0)
counts1, bins1, patches1 = plt.hist(x1)
counts2, bins2, patches2 = plt.hist(x2)

然后将直方图 0 的 bin 位置存储在 bins0 中。

然后将直方图 0 的 bin 中的条目数存储在 counts0 中。

然后我很想将它们收集到二维数组中:

counts = np.vstack([counts0, counts1, counts2]).T
bins = np.vstack([bins0, bins1, bins2]).T

现在,bins[i, j] 详细说明了 bin i 的位置,用于直方图 j。同样counts[i, j] 包含直方图j 的bin i 中的条目数。

通过此设置,您可以获得 bin i 中直方图 0、1 和 2 的计数为 counts[i]

此外,如果您实际上并不需要这些图,并且只调用plt.hist 来处理countsbins,那么您可以改用np.histogram。语法类似:counts, bins = np.histogram(x)np.histogram 不返回补丁)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-22
    • 2016-02-01
    • 1970-01-01
    • 2021-07-22
    • 2013-04-03
    • 2016-10-14
    • 1970-01-01
    • 2019-01-09
    相关资源
    最近更新 更多