1

I am creating a list ans using the following code and have tried to create histogram plots of ans using both matplotlib and seaborn using density=True but I still don't get a probability density of the list of the values in ans. Here is the code.

num=100000
ans=[]
T=4.5*math.pi
num_bins=50
for i in range(num):
    val=0.5*np.random.chisquare(1)+np.random.exponential(1)
    q=np.random.randn()
    repeats=int(T/(2*q))
    listofrepeats=list(itertools.repeat(val,repeats))
    for val in listofrepeats:
        ans.append(val)

#plt.hist(ans,bins=num_bins,normed=True)
sns.distplot(ans, hist=True, kde=False, 
             bins=num_bins, color = 'blue',
             hist_kws={'edgecolor':'black'})
x=np.linspace(0.01,20,1000)
y=[0.5*np.exp(-0.5*n) for n in x]
plt.plot(x,y,lw=3,c='r',label='Chi sqrd with df=2')

plt.legend(loc='upper right')
plt.show()

The density plot I get is enter image description here and as you can see, the density has large values. I am looking for a plotting tool wherein a probability is calculated for each bin and the probabilities sum to one. Is there such a tool? Thanks.

q2w3e4
  • 195
  • 1
  • 1
  • 7
  • 1
    Where is `density=True` in this code? – mkrieger1 Jan 16 '23 at 20:39
  • Does this answer your question? [Normalizing a histogram with matplotlib](https://stackoverflow.com/questions/49781927/normalizing-a-histogram-with-matplotlib) – adrianop01 Jan 16 '23 at 20:39
  • With seaborn you can use `sns.histplot(...., stat='probability')`. See e.g. [Seaborn Plot Distribution with histogram with stat = density or probability?](https://stackoverflow.com/questions/73872722/seaborn-plot-distribution-with-histogram-with-stat-density-or-probability) – JohanC Jan 16 '23 at 21:52
  • Note that `stat='density'` means that the total **area** sums to 1, while `stat='probability'` has the **sum of the bar heights** totaling to 1. Both are equal when all bars would be `1` x unit wide. To fit a normalized curve, you need density, not probability. So, `plt.hist(..., density=True)` or `sns.histplot(..., stat='density')` – JohanC Jan 16 '23 at 21:59
  • @JohanC Thank you, that was helpful. If I use `sns.distplot(...,stat='probability')`, then I get an `unexpected keyword argument` error and if I use `sns.histplot(...,stat='probability')` then I get an error: `module 'seaborn' has no attribute 'histplot'`. Any idea how to get around that? – q2w3e4 Jan 16 '23 at 23:50
  • You need to upgrade to a new seaborn version. `sns.distplot` is rather old and doesn't work with the latest improvements and extensions. – JohanC Jan 16 '23 at 23:56

0 Answers0