plot a normal distribution in python in matlab

Python Code for plotting a Normal Distribution

To plot a normal distribution in python, we need to use the numpy and matplotlib libraries.

main.py
import numpy as np
import matplotlib.pyplot as plt

# Generate some data points
mu, sigma = 0, 0.1  # mean and standard deviation
data_points = np.random.normal(mu, sigma, 1000)

# Plot the histogram of data points
count, bins, ignored = plt.hist(data_points, 30, density=True)

# Plot the normal distribution curve
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ),
         linewidth=2, color='r')

# Add title and labels
plt.title('Normal distribution')
plt.xlabel('X axis')
plt.ylabel('Y axis')

plt.show()
553 chars
21 lines

This will generate a histogram of the data points along with the normal distribution curve plotted on top of it. The mu and sigma variables represent the mean and standard deviation of the normal distribution, respectively. The data_points variable is generated using the numpy.random.normal function which takes in the mu, sigma, and size parameters to generate a set of normally distributed random numbers. The hist function plots the histogram of the data points, while the plot function is used to plot the normal distribution curve using the bins and mu, sigma parameters.

Matlab Code for plotting a Normal Distribution

In Matlab, we can use the normpdf function to plot the normal distribution. Here is the code to plot a normal distribution in Matlab.

main.m
% Generate some data points
mu = 0;
sigma = 0.1;
x = -3:0.1:3;
y = normpdf(x,mu,sigma);

% Plot the normal distribution
plot(x,y,'LineWidth',2)

% Add title and labels
title('Normal distribution')
xlabel('X Axis')
ylabel('Y Axis')
231 chars
14 lines

This will plot the normal distribution curve for the specified mu and sigma values along with the X and Y axis labels. The normpdf function takes in the x, mu, and sigma parameters to compute the normal distribution values for a range of x values, which are then plotted using the plot function.

gistlibby LogSnag