python - 规范化多数据直方图

标签 python numpy matplotlib histogram normalize

我有几个数组正在绘制直方图,如下所示:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.normal(0,.5,1000)
y = np.random.normal(0,.5,100000)

plt.hist((x,y),normed=True)

当然,这会分别对两个阵列进行归一化,因此它们都具有相同的峰值。我希望将它们标准化为元素总数,以便 y 的直方图明显高于 x 的直方图。有没有一种方便的方法可以在 matplotlib 中执行此操作,还是我必须在 numpy 中乱搞?我还没有找到任何相关信息。

另一种说法是,如果我要绘制两个数组的累积图,它们不应都达到 1,而应该加到 1。

最佳答案

是的,您可以使用 numpy 计算直方图并对其进行重新归一化。

x = np.random.normal(0,.5,1000)
y = np.random.normal(0,.5,100000)

xhist, xbins = np.histogram(x, normed=True)
yhist, ybins = np.histogram(x, normed=True)

现在,您应用正则化。例如,如果你希望 x 归一化为 1 并且 y 成比例:

yhist *= len(y) / len(x)

现在,绘制直方图:

def plot_histogram(data, edge_bins, **kwargs):
    bins = edge_bins[:-1] + edge_bins[1:]
    plt.step(bins, data, **kwargs)

plot_histogram(xhist, xbins, c='b')
plot_histogram(yhist, ybins, c='g')

enter image description here

关于python - 规范化多数据直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25252469/

相关文章:

python - 没有名为 appengine.api 的模块

python - C 中的 "Unpacking"

Python 类型错误 : UMat() missing required argument 'ranges' (pos 2)

python - 如何在python中实现队列的深复制

python - 根据另一个列表中的公共(public)值从一个列表中获取值列表的字典

python - 根据另一个数据集中的元素位置过滤 pandas 数据帧的快速方法

python - Matplotlib 绘图窗口未使用 command-w 关闭

python - imshow 并排绘制

python - 使用 Matplotlib 改变 x 轴的比例

python - 在读/写形状文本时保留文本格式 python pptx