How to Plot a Normal Distribution in Python (With Examples)


To plot a normal distribution in Python, you can use the following syntax:

#x-axis ranges from -3 and 3 with .001 steps
x = np.arange(-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1
plt.plot(x, norm.pdf(x, 0, 1))

The x array defines the range for the x-axis and the plt.plot() produces the curve for the normal distribution with the specified mean and standard deviation.

The following examples show how to use these functions in practice.

Example 1: Plot a Single Normal Distribution

The following code shows how to plot a single normal distribution curve with a mean of 0 and a standard deviation of 1:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

#x-axis ranges from -3 and 3 with .001 steps
x = np.arange(-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1
plt.plot(x, norm.pdf(x, 0, 1))

Normal distribution in Python

You can also modify the color and the width of the line in the graph:

plt.plot(x, norm.pdf(x, 0, 1), color='red', linewidth=3)

Example 2: Plot Multiple Normal Distributions

The following code shows how to plot multiple normal distribution curves with different means and standard deviations:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

#x-axis ranges from -5 and 5 with .001 steps
x = np.arange(-5, 5, 0.001)

#define multiple normal distributions
plt.plot(x, norm.pdf(x, 0, 1), label='μ: 0, σ: 1')
plt.plot(x, norm.pdf(x, 0, 1.5), label='μ:0, σ: 1.5')
plt.plot(x, norm.pdf(x, 0, 2), label='μ:0, σ: 2')

#add legend to plot
plt.legend()

Feel free to modify the colors of the lines and add a title and axes labels to make the chart complete:

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

#x-axis ranges from -5 and 5 with .001 steps
x = np.arange(-5, 5, 0.001)

#define multiple normal distributions
plt.plot(x, norm.pdf(x, 0, 1), label='μ: 0, σ: 1', color='gold')
plt.plot(x, norm.pdf(x, 0, 1.5), label='μ:0, σ: 1.5', color='red')
plt.plot(x, norm.pdf(x, 0, 2), label='μ:0, σ: 2', color='pink')

#add legend to plot
plt.legend(title='Parameters')

#add axes labels and a title
plt.ylabel('Density')
plt.xlabel('x')
plt.title('Normal Distributions', fontsize=14)

Refer to the matplotlib documentation for an in-depth explanation of the plt.plot() function.

3 Replies to “How to Plot a Normal Distribution in Python (With Examples)”

  1. Excellent thank you! How do we plot a guassian /bell curve ?
    Also, can we plot a Guassian curve if we only have the mean and the standard deviation (no other data)?

  2. import numpy as np
    import matplotlib.pyplot as plt

    # Parameters of the normal distribution
    mean = 50
    std_dev = 10

    # Generate x values for the distribution curve
    x = np.linspace(mean – 3 * std_dev, mean + 3 * std_dev, 100)

    # Calculate y values for the distribution curve
    y = (1 / (std_dev * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x – mean) / std_dev) ** 2)

    # Plot the distribution curve
    plt.plot(x, y)

    # Shade the area to the left of 40
    x_shade = np.linspace(mean – 3 * std_dev, 40, 100)
    y_shade = (1 / (std_dev * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_shade – mean) / std_dev) ** 2)
    plt.fill_between(x_shade, y_shade, alpha=0.5)

    # Add labels and title
    plt.xlabel(‘Number of Reported Burglaries’)
    plt.ylabel(‘Probability Density’)
    plt.title(‘Distribution of Reported Burglaries in a Month’)

    # Show the plot
    plt.show()

    1. Hi Matebogo…Thank you for you post! Let us know if you have any questions we can help with!

Leave a Reply

Your email address will not be published. Required fields are marked *