规范化多数据直方图

2024-03-28 13:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我绘制了几个数组的柱状图,如下所示:

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)

当然,这会单独规范化两个数组,因此它们都有相同的峰值。{I>{cdm>的元素数明显要比cdm的总数高。在matplotlib中有没有一个方便的方法来完成这项工作呢?还是我必须在numpy中搞乱呢?我没有找到任何关于它的东西。在

另一种说法是,如果我对两个数组做一个累积图,它们不应该都是1,而是应该加到1。在


Tags: importnumpymatplotlibasnp绘制pltrandom
1条回答
网友
1楼 · 发布于 2024-03-28 13:25:39

是的,你可以用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成比例:

^{pr2}$

现在,要绘制直方图:

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

相关问题 更多 >