0
votes

For an assignment I'm asked to plot, in front of a histogram, the histogram of a gaussian distribution. The question is simple, as I already know how to plot the gaussian on top of histograms (and also how to plot histograms).

I already have this:

#bins=array that indicates the boundaries of the bars
#array is an object with attributes such as; standard deviation (stdv); mean or average (mean).

In [63]: 1/(array.stdv*np.sqrt(2*np.pi))*np.exp(-(bins-array.mean)**2/(2*array.stdv**2))
Out[63]: 
array([  1.46863468e-03,   1.71347711e-03,   1.99065837e-03, ...,
         5.37698408e-15,   3.25989619e-15,   1.96798911e-15])

In [64]: bins
Out[64]: array([  33.,   34.,   35., ...,  187.,  188.,  189.])

When I try to plot with this, i get:

In [59]: plt.hist(1/(array.stdv*np.sqrt(2*np.pi))*np.exp(-(bins-array.mean)**2/(2*array.stdv**2)),bins,range=(min(histmain.array),max(histmain.array)),histtype='step')
Out[59]: 
(array([ 0.,  0.,  0., ...,  0.,  0.,  0.]),
 array([  33.,   34.,   35., ...,  187.,  188.,  189.]),
 <a list of 1 Patch objects>)

[Figure][1]

I think I already know the problem, though. I guess the hist function receives an array of data, and it plots according to the frequencies. What I have is an array of the frequencies. I tried multiplying the function by 3200, but what I got was this atrocity:

In [61]: plt.hist(3200/(array.stdv*np.sqrt(2*np.pi))*np.exp(-(bins-array.mean)**2/(2*array.stdv**2)),bins,range=(min(histmain.array),max(histmain.array)))
Out[61]: 
(array([ 1.,  1.,  0., ...,  0.,  0.,  0.]),
 array([  33.,   34.,   35., ...,  187.,  188.,  189.]),
 <a list of 156 Patch objects>)

[Figure][2]

All of the above is without the histogram of data showed.


So sorry. I didn't notice the images weren't displaying. Here's a link to them:

enter image description here

enter image description here

Figure 1 is the flat one. Figure 2 is the horrendous one. What I want to accomplish is a gaussian-like histogram.

1
It doesn't help us if you talk about a problem that is hidden somewhere in some output, which is apparently not what you expect. Please clearly state in how far the output you get is not what you want and what you would like to see instead by editing your question.ImportanceOfBeingErnest
Yeah, thanks for the heads up. The images weren't displaying.condosz
Ok, let me put it differently: A histogram takes the sample data as input and calculates their distribution into bins. If the sample data is from a normal distribution, the histogram will have a gaussian shape. What you are doing here is to put a gaussian shape into the histogram. This makes absolutely no sense at all and since you do not explain what you would expect, I think this question should simply be closed.ImportanceOfBeingErnest
What "star" are you talking about?ImportanceOfBeingErnest

1 Answers

1
votes

Ok, i figured it out myself. This is the algorithm that I used to 'transform' a gaussian distribution into an histogram.

Of course it has yet to be optimized since I just came up with the idea after hours of trial and error.

Let bins and gauss be arrays with the bin distribution and the probability distribution of the left side of each bin respectively.

First, I created a list with the average probability of each bin:

for i in range(len(gauss)):
    try:
        avrgeprob.append((gauss[i]+gauss[i+1])/2)
    except IndexError:
        avrgeprob.append(gauss[i])

Second, I created another list with the expected frequency of each bin (the array that I used to test this had 3200 values in it)

for x in avrgeprob:
    expfreq.append(3200*x)

You can see that

In [121]: sum(expfreq)
Out[121]: 3173.5316995750118

so it's pretty close. Third, I create one last list containing the lower value of each bean as many times as expected

for i in range(len(bins)):
     if int(expfreq[i])!=0:
         fakelist+=[bins[i]]*int(expfreq[i])
     else:
         fakelist.append(0)

After all that, I let the magic happen:

In [122]: plt.hist(fakelist,bins=bins,histtype=u'step')
Out[122]:

enter image description here http://imgur.com/a/uADK4