# How to plot a normal distribution with Matplotlib in Python ?

• Last Updated : 03 Jan, 2021

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.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.78507084,"math_prob":0.9679657,"size":2484,"snap":"2021-43-2021-49","text_gpt3_token_len":504,"char_repetition_ratio":0.123790324,"word_repetition_ratio":0.010638298,"special_character_ratio":0.20008051,"punctuation_ratio":0.12702079,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99968207,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-02T20:17:26Z\",\"WARC-Record-ID\":\"<urn:uuid:3c44e4a4-69a8-4778-8b8b-bf3e6654522a>\",\"Content-Length\":\"114481\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3a290a50-95cd-4b3f-9693-55aa7178ea26>\",\"WARC-Concurrent-To\":\"<urn:uuid:944e6988-30c1-42a7-9faa-a76e59bee47c>\",\"WARC-IP-Address\":\"23.207.202.202\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/how-to-plot-a-normal-distribution-with-matplotlib-in-python/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:DKLIAW77KF32SE4DMSWP7ECMSNAXDOJJ\",\"WARC-Block-Digest\":\"sha1:5YKB5REHOQBMHTW3WKXB567HOAFPVWYF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964362287.26_warc_CC-MAIN-20211202175510-20211202205510-00265.warc.gz\"}"}