【问题标题】:Pyplot Histogram - Get the exact x values from the automatic binsPyplot Histogram - 从自动 bin 中获取准确的 x 值
【发布时间】:2019-03-11 19:10:43
【问题描述】:

我有两个数据数组,我使用 pyplot 为其绘制直方图:

data1 = numpyArray1
data2 = numpyArray2

它们的大小不同,所以我使用选项density=True 来正确比较它们。我还让 pyplot 自动选择 bin,因为数据是浮动的,我不会(除非绝对必要)自动创建限制。

fig, ax = plt.subplots(....)
ax[...].hist([data1, data2], bins = 30, density = True, histtype='step)

例子:

问题:

  1. 我可以假设两个分布的 bin 完全相同吗?
  2. 如何查看(甚至更好地获取)pyplot 创建的自动 bin 限制? (This question assumes the bins are integer numbers, not valid for me)
  3. (可选)我能以某种方式获得两条曲线的交点吗? (This question assumes gaussian distribution, which doesn't solve my problem)

【问题讨论】:

  • 在上面的代码中,您传递了 30 个垃圾箱。根据this 链接,bins 的默认值为 10。

标签: python numpy matplotlib


【解决方案1】:

docshist 将返回

n : 数组或数组列表。直方图 bin 的值。

bins : bins 的边缘。

patches :如果有多个输入数据集,则用于创建直方图或此类列表的列表的单个补丁的静默列表。

所以使用:

freqs, bins, _ = ax[...].hist([data1, data2], bins = 30, density = True, histtype='step)

回答您的问题

我可以假设两个分布的 bin 完全相同吗?

是的,因为它们是在同一个调用中返回的

我怎样才能看到(甚至更好地获得)pyplot 创建的自动 bin 限制? (这个问题假设 bin 是整数,对我无效)

bins(见代码)

(可选)我能以某种方式得到两条曲线的交点吗? (这个问题假设高斯分布,这并不能解决我的问题)

获取频率并将它们归一化,然后查看一个与另一个相交的时间。示例(freq 定义如上):

freqA, freqB = freq
freqA /= freqA.sum()
freqB /= freqB.sum()
ix = np.diff(np.sign(f[0] - f[1])).nonzero()   # check where sign changes (curves cross)
intersections = (bins[ix] + bins[ix + 1]) / 2

【讨论】:

  • 谢谢 :) -- 你知道补丁是什么吗?
  • 它们是 matplotlib.pyplot 对象 (matplotlib.org/api/patches_api.html ),您可以使用它们来访问自定义属性,例如更改其中一个条的颜色。但大多数时候你根本不会使用它们。
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多