Python Forum
Plot Probability Density of an Histogram
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Plot Probability Density of an Histogram
#1
Hi,

Please =i have a code wich plot a histogram for each iteration
and i want to plot the Probability Density of each histogram
how can i do this please ? Huh

you find attached the code


import matplotlib.pyplot as plt
import numpy as np
import pylab 

importdata=open('Hmz_OT_MULC_Discrete5_TOPO.dens','r+')
data=importdata.readlines()
importdata.close()



tri=[]
j=0
for i in range(len(data)):
    if 'iter' in data[i]:
        l=i
        nbre_iter=[]        
        while l+1 < len(data) and 'iter' not in data [l+1]:    
            l+=1
            nbre_iter.append(data[l])  

        tri.append(nbre_iter)
        j+=1


tri_final=[]    
for i in range(len(tri)):
    extract_density=[]   
    for j in range(len(tri[0])):
        extract_density.append(tri[i][j][12 :])
    tri_final.append(extract_density) 
       

convert_into_float=[]    
for i in range(len(tri_final)):
    convert=[]   
    for j in range(len(tri_final[0])):
        convert.append(float(tri_final[i][j]))
    convert_into_float.append(convert)        
 
for i in range(len(convert_into_float)):
    nbre_elements=np.zeros(10)
    for j in range(len(convert_into_float[0])):       
        if 0<= convert_into_float[i][j]< 0.1:
            nbre_elements[0]+=1
        elif 0.1<= convert_into_float[i][j] <0.2:
            nbre_elements[1]+=1
        elif 0.2<= convert_into_float[i][j] <0.3:
            nbre_elements[2]+=1
        elif 0.3<= convert_into_float[i][j] <0.4:
            nbre_elements[3]+=1                         
        elif 0.4<= convert_into_float[i][j] <0.5:
            nbre_elements[4]+=1                               
        elif 0.5<= convert_into_float[i][j] <0.6:
            nbre_elements[5]+=1                
        elif 0.6<= convert_into_float[i][j] <0.7:
            nbre_elements[6]+=1       
        elif 0.7<= convert_into_float[i][j] <0.8:
            nbre_elements[7]+=1
        elif 0.8<= convert_into_float[i][j] <0.9:
            nbre_elements[8]+=1
        elif 0.9<= convert_into_float[i][j] <=1:
            nbre_elements[9]+=1

                                         
    fig, ax = plt.subplots()
    x=[1,2,3,4,5,6,7,8,9,10]
    width = 1.0
    BarName = ['0.0-0.1','0.1-0.2','0.2-0.3','0.3-0.4','0.4-0.5','0.5-0.6','0.6-0.7','0.7-0.8','0.8-0.9','0.9-1']
    rects=plt.bar(x, nbre_elements, width, color='b' )
 
    def autolabel(rects):
        # attach some text labels
        for rect in rects:
            height = rect.get_height()
            ax.text(rect.get_x()+rect.get_width()/2., 1.0*height, '%d'%int(height),
                    ha='center', va='bottom')
    plt.xlabel('Densite')        
    plt.ylabel('Nombre d elements')
    plt.title('NNombre d elements en fonction de leur densite_ITER_'+str(i))
    nom_figure='Histogramme'+str(i)+ '.png'
    pylab.xticks(x, BarName, rotation=40)
    autolabel(rects)
    plt.savefig(nom_figure)
    plt.show()
 
Reply
#2
To plot probability density distribution from empirical data you need to estimate probability density first.
One of the ways to do it is to use the kernel density estimation approach (see scipy's kde density estimator).
Also, you can use ready-made function from seaborn package.
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
x=np.random.randn(10000)                                                                                                                                                                              
sns.distplot(x) #plots histogram and kde-estimation of the pdf.
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  inserting something into probability density SchroedingersLion 1 2,112 Jan-06-2020, 09:15 AM
Last Post: Gribouillis
  Get underlying function from Kernel Density Estimation jpython 3 2,816 Dec-05-2019, 11:23 AM
Last Post: jpython
  How to get the probability density function of my data set jpython 1 2,298 Dec-04-2019, 12:49 PM
Last Post: Larz60+
  finding the integral of probability density function Staph 3 3,647 Aug-11-2019, 09:19 AM
Last Post: buran
  How to fit a 2D histogram like in ROOT? Zandar 2 4,112 Aug-06-2019, 03:01 PM
Last Post: Zandar
  MatPlotLib 2d plot of current density? ruben 0 2,202 May-13-2019, 06:47 AM
Last Post: ruben
  How to check for nested dataframe density? python_newbie09 0 1,973 Aug-27-2018, 07:34 PM
Last Post: python_newbie09
  Draw Weibull distribution probability function based on Confidence interval farzadtb 1 3,697 Jul-31-2018, 03:21 PM
Last Post: Vysero
  histogram error alyssaantarctica 1 3,755 Jul-09-2018, 10:46 PM
Last Post: Larz60+
  histogram with matplotlib vaugirard 10 6,237 Jun-01-2018, 04:13 AM
Last Post: vaugirard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020