Home > Software design >  How to plot a normal pdf in spyder?
How to plot a normal pdf in spyder?

Time:12-12

I am trying to plot a probability density function of a set of datapoints, sampled from a normal distribution as following:

mu, sigma = 0, 1
N = 100000
s = np.random.normal(mu, sigma, N)
def normal_dist(x , mean , sd):
    prob_density = (np.pi*sd) * np.exp(-0.5*((x-mean)/sd)**2)
    return prob_density

pdf = normal_dist(s, mu, sigma)
plt.plot(s,pdf , color = 'red')
plt.xlabel('Data points')
plt.ylabel('Probability Density')
plt.show()

But this is the picture I get. I am interested in getting one line that represents the pdf. Where am i doing wrong?

enter image description here

CodePudding user response:

In pyplot.plot connects one point to the next point in the order in which they occur. This is what you are seeing. The order of your points is random, so you are seeing a bunch of connections between the random points.

Soring s should fix it: s.sort()

  • Related