How to normalize a histogram in python? (updated)
Question: 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 numpy as np
    
     def plotGraph():
    
        import matplotlib.pyplot as plt
    
        k=(1,4,3,1)
    
        plt.hist(k, normed=1)
    
        from numpy import *
        plt.xticks( arange(10) ) # 10 ticks on x axis
    
        plt.show()  
    
    plotGraph()
    

I get this histogram, that doesn't look like normed.

![enter image description here](http://i.stack.imgur.com/t99xD.png)

For a different array k=(3,3,3,3)

    
    
     import numpy as np
    
     def plotGraph():
    
        import matplotlib.pyplot as plt
    
        k=(3,3,3,3)
    
        plt.hist(k, normed=1)
    
        from numpy import *
        plt.xticks( arange(10) ) # 10 ticks on x axis
    
        plt.show()  
    
    plotGraph()
    

I get this histogram with max y-value is 10.

![enter image description here](http://i.stack.imgur.com/8Ag3d.png)

For different k I get different max value of y even though normed=1 or
normed=True.

Why the normalization (if it works) changes based on the data and how can I
make maximum value of y equals to 1?

**UPDATE:**

I am trying to implement [Carsten
König](http://stackoverflow.com/users/1542814/carsten-konig) answer from
[plotting histograms whose bar heights sum to 1 in
matplotlib](http://stackoverflow.com/questions/3866520/plotting-histograms-
whose-bar-heights-sum-to-1-in-matplotlib) and getting very weird result:

    
    
    import numpy as np
    
    def plotGraph():
    
        import matplotlib.pyplot as plt
    
        k=(1,4,3,1)
    
        weights = np.ones_like(k)/len(k)
        plt.hist(k, weights=weights)
    
        from numpy import *
        plt.xticks( arange(10) ) # 10 ticks on x axis
    
        plt.show()  
    
    plotGraph()
    

Result:

![enter image description here](http://i.stack.imgur.com/FVmUu.png)

What am I doing wrong?

Thanks


Answer: When you plot a normalized histogram, it is not the height that should sum up
to one, but the area underneath the curve should sum up to one:

    
    
    In [44]:
    
    import matplotlib.pyplot as plt
    k=(3,3,3,3)
    x,bins,p=plt.hist(k, normed=1)
    from numpy import *
    plt.xticks( arange(10) ) # 10 ticks on x axis
    plt.show()  
    In [45]:
    
    print bins
    [ 2.5  2.6  2.7  2.8  2.9  3.   3.1  3.2  3.3  3.4  3.5]
    

Here, this example, the bin width is 0.1, the area underneath the curve sums
up to one (0.1*10).

To have the sum of height to be 1, add the following before `plt.show()`:

    
    
    for item in p:
        item.set_height(item.get_height()/sum(x))
    

![enter image description here](http://i.stack.imgur.com/AIjXM.png)

