Normal Distribution in Python is a distribution where most of the datasets are located in the middle range. It is symmetric from the middle. There are many important features of a normal distribution. Whether you are a statistic student or a computer science student, you will get to know the normal distribution a lot in your academic journey. In this post, we are going to plot normal distribution in Python using various modules. We will use Matplotlib, Seaborn, or Plotly modules to plot the distribution. Moreover, we will also learn how to create a normally distributed dataset in Python as well.
How to Plot Normal Distribution in Python?
Before going to plot the graph of the normal distribution, we need to understand what is the normal distribution and why it is so important. Well, a normal distribution is a probabilistic distribution that shows that most of the dataset is found in the middle section, while a very small portion of the data is scattered away from the mean. Here are some of the features of a normal distribution:
- The distribution is symmetric.
- The normal distribution has only a single mode
- It is a bell-shaped curve so it is easy to find out normal distributed data.
- The mean is in the center
- The normal distribution follows the empirical rule.
- Cumulative Distribution Function
- Z-score is used to standardize values from the normal distribution
- And many other features
Here is the simple formula for the normal distribution:
f(x | μ, σ) = (1 / σ√(2π)) * e^(-0.5 * ((x – μ) / σ)^2)
How to Create a Normal Distributed Dataset in Python?
Before starting working with the normal distribution in Python, make sure that you have installed the following modules as we are going to use them to create and plot normal distribution.
- Matplotlib module
- Seaborn module
You can use the pip command to install these modules on your system.
Once the installation is complete, the first step is to create the normal distributed dataset.
# importing the module import numpy as np # creating dataset in specific range dataset = np.arange(-10, 10, 0.01)
Once the dataset is ready, we can start our plotting process.
Plot Normal Distribution in Matplotlib
We know that to plot the graph, we need to find the mean and standard deviation of the dataset. Let us first calculate them.
import statistics # mean and standard deviation mean = statistics.mean(dataset) sd = statistics.stdev(dataset)
Now we have everything to plot the normal distribution. Let us import the Matplotlib module and show the graph. The norm.pdf() method in the Scipy module is used to create the normally distributed data by taking the mean and standard deviation as parameter values.
# importing module import matplotlib.pyplot from scipy.stats import norm plt.plot(dataset, norm.pdf(dataset, mean, sd)) plt.show()
As you can see, we plotted the graph for the given dataset. Here are the simple steps you need to follow in order to plot the normal distribution dataset in Python using any module.
- Import the dataset
- Find the mean and standard deviation of the dataset
- Use the formula to find the normal distribution
- Plot the distribution
- You are all done!
How to Plot Normal Distribution in Seaborn?
Seaborn is another important Python module that is used to create colorful graphs. We will now use the seaborn to plot the graph of normal distribution.
# impoting the module import seaborn as sns #create data x = np.random.normal(size=100) #normal distribution curve sns.displot(x, kind='kde')
As you can see, we plotted the distribution in Seaborn.
Normal Distribution With Histogram Plot
So far, we have used the curve graph to plot the normal distribution, now we will plot the distribution using the histograms. The process is very similar to the previous one, all we need is to change the plotting type from curve to histogram.
#create data x = np.random.normal(size=10000) #histogram sns.displot(x)
As you can see, this time the graph is made of histograms.
Normal Distribution With Curve and Histogram
So far, we have learned how we can plot the normal distribution graph using curves and histograms. Now, we will combine both and plot them together on one graph.
#create data x = np.random.normal(size=10000) #create normal distribution curve sns.displot(x, kde=True)
Hopefully, this tutorial was helpful and you learned the plotting of normal distribution using various methods.
Normal Distribution is a type of distribution where the data is normally scattered which means most of the data points are in the middle and very few are scattered away from the middle. We used the Matplotlib and Seaborn modules to plot the graph of the normal distribution taking various examples.
You May Also Like
- How to Change Font Size in Matplotlib? Examples
- How to Save Plot as Image in Matplotlib?
- Data Visualization Using Pandas