Aida
Aida

Reputation: 2434

Matlab: How to show sigma and mu on normal curve

I am plotting a normal curve by using y = normpdf(x,mu,sigma); in Matlab. This draws for me a normal curve. But, I need additional information to be shown on the curve, like having vertical lines on the curve to show the mu and the sigma. Similar to this one: enter image description here

Is there any Matlab function to draw such vertical lines on the curve?

Thanks, Aida

Upvotes: 1

Views: 2183

Answers (2)

Christof Vermeersch
Christof Vermeersch

Reputation: 495

The easiest way to draw lines in Matlab is probably using simply plot. The following code segment draws a line from [x1, y1] to [x2, y2]:

plot([x1,y1], [x2,y2], plotoptions)

To make sure that this line is also visible on your current figure use hold on. This results in a code like the following:

y = normpdf(x,mu,sigma)
figure(1)
    hold on
    plot(x,y) % Your normal distribution
    plot([mu,startY], [mu, stopY], plotoptions) % Plot the line at position mu.
                                                % The startY and stopY indicate the bottom and top of your graph.
    hold off

The Mathworks documentation page of plot can be a great reference, with a lot of examples and different plot options. For example how to make a dashed or colored line: http://www.mathworks.com/help/matlab/ref/plot.html?searchHighlight=plot%20line

Upvotes: 0

Mikhail_Sam
Mikhail_Sam

Reputation: 11248

There are no built-in function for this, but we can do it easily by the hands:

Create normal curve and plot it:

x = -2:0.05:2;
mu = 0; sigma = 0.5;
y = normpdf(x,mu,sigma);
plot(x,y)

Add lines for sigmas:

hold on;
plot( [mu - sigma mu - sigma],[0 max(y)],'--')
plot( [mu + sigma mu + sigma],[0 max(y)],'--')

You can change it to any sigma you need (2sigma 3sigma). How to add the text? This way:

text(0.1,-0.05,'mu + sigma');

or if you want it look really beautiful:

text(-0.65,-0.05,'\mu - \sigma')

Result:

enter image description here

Upvotes: 5

Related Questions