【问题标题】:Plot a histogram such that bar heights sum to 1 (probability)绘制直方图,使条形高度总和为 1(概率)
【发布时间】:2011-04-21 11:01:00
【问题描述】:

我想使用matplotlib 从向量中绘制归一化直方图。我尝试了以下方法:

plt.hist(myarray, normed=True)

还有:

plt.hist(myarray, normed=1)

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

【问题讨论】:

标签: python matplotlib seaborn histogram


【解决方案1】:
  • 最简单的解决方案是使用seaborn.histplot,或seaborn.displotkind='hist',并指定stat='probability'
    • 概率:或比例:标准化,使条形高度总和为 1
    • 密度:归一化,使得直方图的总面积等于 1
    • data:pandas.DataFramenumpy.ndarray、映射或序列
  • seabornmatplotlib 的高级 API
  • python 3.8.12matplotlib 3.4.3seaborn 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)

【讨论】:

    【解决方案2】:

    自 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() 的参数对我不起作用。如果有人知道如何在没有访问规范关键字参数的情况下使其正常工作,请在 cmets 中告诉我,我将删除/修改此答案。

    如果您想要 bin 中心,则不要使用作为 bin 边缘的 bins[:-1] - 您需要选择一个合适的方案来计算中心(可能会或可能不会简单地导出)。

    【讨论】:

      【解决方案3】:

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

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

      希望对您有所帮助,虽然线程很旧......

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

      【讨论】:

      • 很好的答案。请注意,如果 myarray 是 python array_like 而不是 numpy 数组,则需要将 len(myarray) 转换为 float
      • 另外,如果 myarray 是多维的并且您只使用一维,例如 myarray[0,:],那么您可以将 len(myarray) 替换为 np.size(myarray[0,: ]) 并且以同样的方式工作。 (否则,它表示该对象不可调用。)
      • 这很有帮助。谢谢。
      【解决方案4】:

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

      我尝试了以下方法:

      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 文档(即来自ipythonax.hist?),我认为总和也不错:

      *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

      【讨论】:

      • 是的,这是一个概率密度图,我想他想要一个概率质量图。
      【解决方案5】:

      这是另一个使用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
      

      【讨论】:

        【解决方案6】:

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

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

        但我无法找到使条形图模仿 histt​​ype="step" 功能 hist() 的方法。这让我转向:Matplotlib - Stepped histogram with already binned data

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

        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 即可获得必要的结果。

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

        【讨论】:

        • 我认为您在 plt.hist 中也需要 normed=True。同样在 Python 3 中,您必须使用 list(zip(...))。
        猜你喜欢
        • 1970-01-01
        • 2013-06-29
        • 2014-04-10
        • 2012-11-09
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多