1.6.12.7. Normal distribution: histogram and PDFΒΆ

Explore the normal distribution: a histogram built from samples and the PDF (probability density function).

../../../_images/sphx_glr_plot_normal_distribution_001.png
import numpy as np
# Sample from a normal distribution using numpy's random number generator
samples = np.random.normal(size=10000)
# Compute a histogram of the sample
bins = np.linspace(-5, 5, 30)
histogram, bins = np.histogram(samples, bins=bins, density=True)
bin_centers = 0.5*(bins[1:] + bins[:-1])
# Compute the PDF on the bin centers from scipy distribution object
from scipy import stats
pdf = stats.norm.pdf(bin_centers)
from matplotlib import pyplot as plt
plt.figure(figsize=(6, 4))
plt.plot(bin_centers, histogram, label="Histogram of samples")
plt.plot(bin_centers, pdf, label="PDF")
plt.legend()
plt.show()

Total running time of the script: ( 0 minutes 0.014 seconds)

Gallery generated by Sphinx-Gallery