繁体   English   中英

在 Matplotlib 的直方图中调用单个 bin?

[英]Call upon individual bins in histograms in Matplotlib?

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

有什么方法可以从直方图中调用各个 bin 来使用吗?

提前致谢

是的,当您调用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]详细说明了直方图j中 bin i的位置。 同样counts[i, j]包含直方图j bin i中的条目数。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM