3

I would like to add a standard normal pdf curve over a histogram built with seaborn.

import numpy as np
import seaborn as sns 
x = np.random.standard_normal(1000)
sns.distplot(x, kde = False)

Any help would be appreciated!

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
abu
  • 737
  • 5
  • 8
  • 19

1 Answers1

17
  • scipy.stats.norm gives easy access to the pdf of a normal distribution with
    known parameters; by default it corresponds to the standard normal, mu=0, sigma=1.
    • This answer works regardless of where the data mean is located (e.g. mu=0 or mu=10)
  • Tested in python 3.8.11, matplotlib 3.4.2, seaborn 0.11.2
  • This question and answer are for axes-level plots; for figure-level plots, see How to draw a normal curve on seaborn displot

Imports and Data

import numpy as np                                                              
import seaborn as sns                                                           
from scipy import stats                                                         
import matplotlib.pyplot as plt  

# data
np.random.seed(365)
x = np.random.standard_normal(1000)    

seaborn.histplot

ax = sns.histplot(x, kde=False, stat='density', label='samples')

# calculate the pdf
x0, x1 = ax.get_xlim()  # extract the endpoints for the x-axis
x_pdf = np.linspace(x0, x1, 100)
y_pdf = scipy.stats.norm.pdf(x_pdf)

ax.plot(x_pdf, y_pdf, 'r', lw=2, label='pdf')                                                   
ax.legend()

enter image description here

seaborn.distplot - deprecated

  • For this to correspond correctly to your sampled data, the histogram should
    display densities and not counts, so use norm_hist=True in the seaborn.distplot call.
ax = sns.distplot(x, kde = False, norm_hist=True, hist_kws={'ec': 'k'}, label='samples')

# calculate the pdf
x0, x1 = ax.get_xlim()  # extract the endpoints for the x-axis
x_pdf = np.linspace(x0, x1, 100)
y_pdf = scipy.stats.norm.pdf(x_pdf)

ax.plot(x_pdf, y_pdf, 'r', lw=2, label='pdf')                                                     
ax.legend()

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Bonlenfum
  • 19,101
  • 2
  • 53
  • 56
  • Why does your plot only go from 0.0 to 0.4? How do I draw a normal distribution if if have a y-axis with a density that goes from 0 to 8? – Jiren Dec 18 '21 at 11:43
  • @Jiren **density**: normalize such that the total area of the histogram equals 1. You probably want this [question](https://stackoverflow.com/q/64621456/7758804) – Trenton McKinney Dec 19 '21 at 18:35