Hackerss.com

hackerss
hackerss

Posted on

Plot a normal distribution in Python

Plot a normal distribution - Python:


import matplotlib.pyplot as plt
import numpy as np

def plot_normal_distribution(mean, std):
    """
    Plot a normal distribution.

    Parameters
    ----------
    mean : float
        Mean of the normal distribution.
    std : float
        Standard deviation of the normal distribution.

    Returns
    -------
    None
        The function plots the normal distribution and does not return anything.
    """

    #create a range of x values from -4σ to 4σ with 0.01 increments
    x = np.arange(mean - 4*std, mean + 4*std, 0.01)

    #calculate the y values corresponding to the x values
    y = (1/(std * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mean)/std)**2)

    #plot the x and y values
    plt.plot(x, y)

    #show the plot
    plt.show()

#example
plot_normal_distribution(0, 1)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)