Plot Probability Density of an Histogram - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding) +--- Forum: Data Science (https://python-forum.io/Forum-Data-Science) +--- Thread: Plot Probability Density of an Histogram (/Thread-Plot-Probability-Density-of-an-Histogram) Plot Probability Density of an Histogram - Python_TLS - Jun-27-2018 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 ? 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() ``` RE: Plot Probability Density of an Histogram - scidam - Jun-28-2018 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()```