plot normal distribution given mean and sigma - python

15,600

You can use matplotlib/pylab with scipy.stats.norm.pdf and pass the mean and standard deviation as loc and scale:

import pylab
import numpy as np
from scipy.stats import norm
x = np.linspace(-10,10,1000)
y = norm.pdf(x, loc=2.5, scale=1.5)    # for example
pylab.plot(x,y)
pylab.show()

enter image description here

Share:
15,600
Admin
Author by

Admin

Updated on June 29, 2022

Comments

  • Admin
    Admin almost 2 years

    I have some data in pandas dataframe

    df['Difference'] = df.Congruent.values - df.Incongruent.values
    mean = df.Difference.mean()
    std = df.Difference.std(ddof=1)
    median = df.Difference.median()
    mode = df.Difference.mode()
    

    and I want to plot a histogram together with normal distribution in 1 plot. Is there a plotting function that takes mean and sigma as arguments? I don't care whether it is matplotplib, seaborn or ggplot. The best would be if I could mark also mode and median of the data all within 1 plot.

  • Admin
    Admin almost 9 years
    How can I manipulate the max value of this dist? Because when I plot it together with histogram, the normal dist. flattens out and thus looks like a straight line at the bottom.
  • xnx
    xnx almost 9 years
    Either multiply it by a constant (but then it won't be a normalized distribution), or normalize your histogram (I think there's a normed=True argument in matplotlib).
  • Admin
    Admin almost 9 years
    ok got it with 'normed=true' as you said. Tho, the best would be 2 axes on two sided, one for histogram and one for distr. Do you know how to do this?
  • xnx
    xnx almost 9 years
    You probably want twinx as in this example
  • mwaskom
    mwaskom almost 9 years
    Because of the auto-scaling of the axes to get "nice" tick values, just using twinx is probably going to result in a plot that isn't quite accurate in terms of equating the normalized density and count values along the vertical axis of the figure.
  • xnx
    xnx almost 9 years
    True... though I think you could normalize the data, twin the x-axis for the plotted distribution and then set both y-axis limits to be the same.