93

我想使用matplotlib. 我尝试了以下方法:

plt.hist(myarray, normed=True)

也:

plt.hist(myarray, normed=1)

但是这两个选项都不会从 [0, 1] 生成 y 轴,这样直方图的条形高度总和为 1。

4

6 回答 6

224

如果您希望所有条形的总和相等,请按值的总数对每个 bin 加权:

weights = np.ones_like(myarray) / len(myarray)
plt.hist(myarray, weights=weights)

希望有帮助,虽然线程很老......

Python 2.x 的注意事项:float()为除法运算符之一添加强制转换,否则由于整数除法,您最终会得到零

于 2013-05-06T12:53:31.897 回答
49

如果您提出一个更完整的工作(或在这种情况下不工作)示例,这将更有帮助。

我尝试了以下方法:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randn(1000)

fig = plt.figure()
ax = fig.add_subplot(111)
n, bins, rectangles = ax.hist(x, 50, density=True)
fig.canvas.draw()
plt.show()

这确实会产生一个条形图直方图,其 y 轴从[0,1].

此外,根据hist文档(即ax.hist?来自ipython),我认为总和也很好:

*normed*:
If *True*, the first element of the return tuple will
be the counts normalized to form a probability density, i.e.,
``n/(len(x)*dbin)``.  In a probability density, the integral of
the histogram should be 1; you can verify that with a
trapezoidal integration of the probability density function::

    pdf, bins, patches = ax.hist(...)
    print np.sum(pdf * np.diff(bins))

在上面的命令之后尝试一下:

np.sum(n * np.diff(bins))

我得到了1.0预期的返回值。请记住,normed=True这并不意味着每个条上的值之和是统一的,而是条上的积分是统一的。在我的情况下np.sum(n)返回约7.2767

于 2010-10-05T18:45:29.997 回答
23

考虑到这个问题的日期是 2010 年,我知道这个答案为时已晚,但我遇到了这个问题,因为我自己也面临着类似的问题。如答案中所述, normed=True 表示直方图下的总面积等于 1,但高度之和不等于 1。但是,为了方便对直方图进行物理解释,我想制作一个高度之和等于 1。

我在以下问题中找到了提示 - Python: Histogram with area normalized to something other than 1

但我无法找到一种让条形图模仿 histt​​ype="step" 特征 hist() 的方法。这让我转向:Matplotlib - 已分箱数据的阶梯直方图

如果社区认为它可以接受,我想提出一个综合上述两个帖子的想法的解决方案。

import matplotlib.pyplot as plt

# Let X be the array whose histogram needs to be plotted.
nx, xbins, ptchs = plt.hist(X, bins=20)
plt.clf() # Get rid of this histogram since not the one we want.

nx_frac = nx/float(len(nx)) # Each bin divided by total number of objects.
width = xbins[1] - xbins[0] # Width of each bin.
x = np.ravel(zip(xbins[:-1], xbins[:-1]+width))
y = np.ravel(zip(nx_frac,nx_frac))

plt.plot(x,y,linestyle="dashed",label="MyLabel")
#... Further formatting.

这对我来说非常有效,尽管在某些情况下我注意到直方图最左边的“条”或最右边的“条”不会通过触摸 Y 轴的最低点而关闭。在这种情况下,在 begging 或 y 末尾添加元素 0 可以获得必要的结果。

只是想我会分享我的经验。谢谢你。

于 2013-02-27T19:57:27.400 回答
14

这是另一个使用np.histogram()方法的简单解决方案。

myarray = np.random.random(100)
results, edges = np.histogram(myarray, normed=True)
binWidth = edges[1] - edges[0]
plt.bar(edges[:-1], results*binWidth, binWidth)

您确实可以通过以下方式检查总和是否为 1:

> print sum(results*binWidth)
1.0
于 2015-07-18T01:12:23.030 回答
4
  • 最简单的解决方案是使用seaborn.histplot, 或seaborn.displotwith kind='hist', 并指定stat='probability'
    • 概率:或比例:归一化,使条形高度总和为 1
    • 密度:归一化,使得直方图的总面积等于 1
    • data: pandas.DataFrame, numpy.ndarray, 映射或序列
  • seaborn是一个高级 APImatplotlib
  • python 3.8.12, matplotlib 3.4.3,中测试seaborn 0.11.2

导入和数据

import seaborn as sns
import matplotlib.pyplot as plt

# load data
df = sns.load_dataset('penguins')

sns.histplot

  • 轴级图
# create figure and axes
fig, ax = plt.subplots(figsize=(6, 5))

p = sns.histplot(data=df, x='flipper_length_mm', stat='probability', ax=ax)

在此处输入图像描述

sns.displot

  • 图形级图
p = sns.displot(data=df, x='flipper_length_mm', stat='probability', height=4, aspect=1.5)

在此处输入图像描述

于 2021-11-21T19:03:42.980 回答
0

自 matplotlib 3.0.2 以来,normed=True已弃用。为了获得所需的输出,我必须这样做:

import numpy as np
data=np.random.randn(1000)
bins=np.arange(-3.0,3.0,51)
counts, _ = np.histogram(data,bins=bins)
if density: # equivalent of normed=True
    counts_weighter=counts.sum()
else: # equivalent of normed=False
    counts_weighter=1.0
plt.hist(bins[:-1],bins=bins,weights=counts/counts_weighter)

试图指定weightsdensity同时作为参数plt.hist()对我不起作用。如果有人知道如何在无法访问规范关键字参数的情况下使其正常工作,请在评论中告诉我,我将删除/修改此答案。

如果您想要 bin 中心,则不要使用bins[:-1]哪些是 bin 边缘 - 您需要选择一个合适的方案来计算中心(这可能是也可能不是微不足道的派生)。

于 2021-06-04T10:16:59.210 回答