matplotlib - How to normalize a histogram in python? -


i'm trying plot normed histogram, instead of getting 1 maximum value on y axis, i'm getting different numbers.

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

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

i histogram, doesn't normed.

enter image description here

for different array k=(3,3,3,3)

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

i histogram max y-value 10.

enter image description here

for different k different max value of y though normed=1 or normed=true.

why normalization (if works) changes based on data , how can make maximum value of y equals 1?

update:

i trying implement carsten könig answer plotting histograms bar heights sum 1 in matplotlib , getting weird result:

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

result:

enter image description here

what doing wrong?

thanks

when plot normalized histogram, not height should sum one, area underneath curve should sum one:

in [44]:  import matplotlib.pyplot plt k=(3,3,3,3) x,bins,p=plt.hist(k, normed=1) 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, example, bin width 0.1, area underneath curve sums 1 (0.1*10).

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

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

enter image description here


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

ios - Can NSManagedObject conform to NSCoding -