The Wayback Machine - https://web.archive.org/web/20240815013212/https://www.geeksforgeeks.org/how-to-plot-a-normal-distribution-with-matplotlib-in-python/
Open In App

How to plot a normal distribution with Matplotlib in Python ?

Last Updated : 03 Jan, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisites:

Normal Distribution is a probability function used in statistics that tells about how the data values are distributed. It is the most important probability distribution function used in statistics because of its advantages in real case scenarios. For example, the height of the population, shoe size, IQ level, rolling a die, and many more. 

The probability density function of normal or Gaussian distribution is given by:

Probability Density Function

Where, x is the variable, mu is the mean, and sigma standard deviation

Modules Needed

  • Matplotlib is python’s data visualization library which is widely used for the purpose of data visualization.
  • Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
  • Scipy is a python library that is useful in solving many mathematical equations and algorithms.
  • Statistics module provides functions for calculating mathematical statistics of numeric data.

Functions used

  • To calculate mean of the data

Syntax:

mean(data)
  • To calculate standard deviation of the data

Syntax:

stdev(data)
  • To calculate normal probability density of the data norm.pdf is used, it refers to the normal probability density function which is a module in scipy library that uses the above probability density function to calculate the value.

Syntax:

norm.pdf(Data, loc, scale)

Here, loc parameter is also known as the mean and the scale parameter is also known as standard deviation.

Approach

  • Import module
  • Create data
  • Calculate mean and deviation
  • Calculate normal probability density
  • Plot using above calculated values
  • Display plot

Below is the implementation.

Python3




import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
import statistics
  
# Plot between -10 and 10 with .001 steps.
x_axis = np.arange(-20, 20, 0.01)
  
# Calculating mean and standard deviation
mean = statistics.mean(x_axis)
sd = statistics.stdev(x_axis)
  
plt.plot(x_axis, norm.pdf(x_axis, mean, sd))
plt.show()


Output:

The output of above code



Similar Reads

Normal Distribution Plot using Numpy and Matplotlib
In this article, we will see how we can create a normal distribution plot in python with numpy and matplotlib module.  What is Normal Distribution?Normal Distribution is a probability function used in statistics that tells about how the data values are distributed. It is the most important probability distribution function used in statistics becaus
3 min read
How to Plot Normal Distribution over Histogram in Python?
In this article, we will discuss how to Plot Normal Distribution over Histogram using Python. First, we will discuss Histogram and Normal Distribution graphs separately, and then we will merge both graphs together.  HistogramA histogram is a graphical representation of a set of data points arranged in a user-defined range. Similar to a bar chart, a
4 min read
How to calculate and plot a Cumulative Distribution function with Matplotlib in Python ?
Prerequisites: Matplotlib Matplotlib is a library in Python and it is a numerical — mathematical extension for the NumPy library. The cumulative distribution function (CDF) of a real-valued random variable X, or just distribution function of X, evaluated at x, is the probability that X will take a value less than or equal to x. Properties of CDF: E
3 min read
Python - Log Normal Distribution in Statistics
scipy.stats.lognorm() is a log-Normal continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default = 0 scal
2 min read
Python - Power Log-Normal Distribution in Statistics
scipy.stats.powerlognorm() is a power log-normal continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Defau
2 min read
Python - Normal Inverse Gaussian Distribution in Statistics
scipy.stats.norminvgauss() is a Normal Inverse Gaussian continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter
2 min read
Python - Skew-Normal Distribution in Statistics
scipy.stats.skewnorm() is a skew-normal continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default = 0 sc
2 min read
Python - Power Normal Distribution in Statistics
scipy.stats.powernorm() is a power normal continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default = 0
2 min read
Python - Truncated Normal Distribution in Statistics
scipy.stats.truncnorm() is a Truncated Normal continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. Parameters : q : lower and upper tail probability x : quantiles loc : [optional]location parameter. Default
2 min read
How to calculate probability in a normal distribution given mean and standard deviation in Python?
A normal distribution is a type of continuous probability distribution for a real-valued random variable. It is based on mean and standard deviation. The probability distribution function or PDF computes the likelihood of a single point in the distribution. The general formula to calculate PDF for the normal distribution is [Tex] f_X(x) = \frac{1}{
2 min read
three90RightbarBannerImg