How to normalize a histogram in python?

前端 未结 5 1154
星月不相逢
星月不相逢 2021-02-12 11:35

I\'m trying to plot normed histogram, but instead of getting 1 as maximum value on y axis, I\'m getting different numbers.

For array k=(1,4,3,1)

 import         


        
5条回答
  •  爱一瞬间的悲伤
    2021-02-12 11:48

    A normed histogram is defined such that the sum of products of width and height of each column is equal to the total count. That's why you are not getting your max equal to one.

    However, if you still want to force it to be 1, you could use numpy and matplotlib.pyplot.bar in the following way

    sample = np.random.normal(0,10,100)
    #generate bins boundaries and heights
    bin_height,bin_boundary = np.histogram(sample,bins=10)
    #define width of each column
    width = bin_boundary[1]-bin_boundary[0]
    #standardize each column by dividing with the maximum height
    bin_height = bin_height/float(max(bin_height))
    #plot
    plt.bar(bin_boundary[:-1],bin_height,width = width)
    plt.show()
    

提交回复
热议问题