首页 文章

将matplotlib直方图除以最大bin值

提问于
浏览
2

我想在同一个图上绘制多个直方图,我需要比较数据的传播 . 我想通过将每个直方图除以其最大值来做到这一点,因此所有分布都具有相同的比例 . 然而,matplotlib的直方图函数的工作方式,我还没有找到一个简单的方法来做到这一点 .

这是因为n in

n, bins, patches = ax1.hist(y, bins = 20, histtype = 'step', color = 'k')

是每个bin中的计数数量,但我无法将其重新计算为hist,因为它将重新计算 .

我已经尝试了范数和密度函数,但是这些函数规范了分布的面积,而不是分布的高度 . 我可以复制n然后使用bin输出重复bin边缘,但这很乏味 . 当然,hist函数必须允许将bin值除以常数?

示例代码如下,证明了问题 .

y1 = np.random.randn(100)
y2 = 2*np.random.randn(50)
x1 = np.linspace(1,101,100)
x2 = np.linspace(1,51,50)
gs = plt.GridSpec(1,2, wspace = 0, width_ratios = [3,1])
ax = plt.subplot(gs[0])
ax1 = plt.subplot(gs[1])
ax1.yaxis.set_ticklabels([])   # remove the major ticks

ax.scatter(x1, y1, marker='+',color = 'k')#, c=SNR, cmap=plt.cm.Greys)
ax.scatter(x2, y2, marker='o',color = 'k')#, c=SNR, cmap=plt.cm.Greys)
n1, bins1, patches1 = ax1.hist(y1, bins = 20, histtype = 'step', color = 'k',linewidth = 2, orientation = 'horizontal')
n2, bins2, patched2 = ax1.hist(y2, bins = 20, histtype = 'step', linestyle = 'dashed', color = 'k', orientation = 'horizontal')

Example output. I want the max bins of the dashed and dotted lines to be 1.

4 回答

  • 0

    我不知道默认情况下matplotlib是否允许这种规范化,但我自己编写了一个函数 .

    它从plt.hist(如上所示)获取 nbins 的输出,然后通过下面的函数传递它 .

    def hist_norm_height(n,bins,const):
        ''' Function to normalise bin height by a constant. 
            Needs n and bins from np.histogram or ax.hist.'''
    
        n = np.repeat(n,2)
        n = float32(n) / const
        new_bins = [bins[0]]
        new_bins.extend(np.repeat(bins[1:],2))
        return n,new_bins[:-1]
    

    要立即绘制(我喜欢步骤直方图),您将它传递给plt.step .

    plt.step(new_bins,n) . 这将为您提供一个高度标准化的高度直方图 .

  • 0

    您可以将参数 bins 指定为值列表 . 使用 np.arange()np.linspace() 生成值 . http://matplotlib.org/api/axes_api.html?highlight=hist#matplotlib.axes.Axes.hist

  • 0

    为比较设置了略有不同的方法 . 可以适应步骤样式:

    # -*- coding: utf-8 -*-
    import matplotlib.pyplot as plt
    import numpy as np
    
    y = []
    y.append(np.random.normal(2, 2, size=40))
    y.append(np.random.normal(3, 1.5, size=40))
    y.append(np.random.normal(4,4,size=40))
    ls = ['dashed','dotted','solid']
    
    fig, (ax1, ax2, ax3) = plt.subplots(ncols=3)
    for l, data in zip(ls, y):
        n, b, p = ax1.hist(data, normed=False,
                           #histtype='step', #step's too much of a pain to get the bins
                           #color='k', linestyle=l,
                           alpha=0.2
                           )
        ax2.hist(data, normed=True,
                 #histtype = 'step', color='k', linestyle=l,
                 alpha=0.2
                 )
    
        n, b, p = ax3.hist(data, normed=False,
                           #histtype='step', #step's too much of a pain to get the bins
                           #color='k', linestyle=l,
                           alpha=0.2
                           )
        high = float(max([r.get_height() for r in p]))
        for r in p:
            r.set_height(r.get_height()/high)
            ax3.add_patch(r)
        ax3.set_ylim(0,1)
    
    ax1.set_title('hist')
    ax2.set_title('area==1')
    ax3.set_title('fix height')
    plt.show()
    

    一对夫妇的产出:

    enter image description here

    enter image description here

    enter image description here

  • 1

    这可以使用 numpy 来获得先验直方图值,然后用bar plot绘制它们 .

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Define random data and number of bins to use
    x = np.random.randn(1000)
    bins = 10
    
    plt.figure()
    # Obtain the bin values and edges using numpy
    hist, bin_edges = np.histogram(x, bins=bins, density=True)
    # Plot bars with the proper positioning, height, and width.
    plt.bar(
        (bin_edges[1:] + bin_edges[:-1]) * .5, hist / hist.max(),
        width=(bin_edges[1] - bin_edges[0]), color="blue")
    
    plt.show()
    

    enter image description here

相关问题