是的,当您调用 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 来处理counts 和bins,那么您可以改用np.histogram。语法类似:counts, bins = np.histogram(x)(np.histogram 不返回补丁)。