help-octave
[Top][All Lists]

## RE: Combining histograms and function plots

 From: Ted Harding Subject: RE: Combining histograms and function plots Date: Wed, 29 Jan 2003 17:46:16 -0000 (GMT)

```On 29-Jan-03 Toke Reichstein wrote:
> I am making histogram plots of some simulation generated data.
> These are supposed to be normally distributed. I would like to
> combine my formal statistical test with a visual plot of the data.
> But on top of the histogram I would also like to have the normal
> distribution function drawn. It is in this resepct I am experiencing
> problems.
>
> Q1: How do I plot a normal distribution function with the mean and
> standard deviation of my data in Octave?
>
> Q2: How do I plot it ontop of my histogram plots?

You could try something along the following lines.

Q1: Suppose the mean is "m" and the SD is "s".
s2 = s*s; dx = 0.01;
x = dx*(-300:300);
y = (1/sqrt(2*pi*s2))*exp(-0.5*(x.*x)/s2);

plot(x+m,y)

will give you a plot of the normal distribution.

Q2: (See "help hist" and "help bar")
Suppose your N data are in vector Y and you have binned them
in, say,

binw = s/5;
bins = m + binw*(-10:10);

(i.e. 21 bins of width binw, 10 bins on either side of the mean;
but you can vary this to taste)

[NN, XX] = hist (Y, bins);
[xb,yb] = bar(XX,NN);
plot(xb,yb,"-b", x+m,(N*binw)*y,"-g")

will give you the histogram in blue and the fitted normal
distribution in green. Adjust "dx", "binw", etc. to taste.

For example:

N = 5000; rand("normal"); Y=3+1.3*rand(5000,1);
m = mean(Y); s = std(Y);
s2 = s*s; dx = 0.01;
x = dx*(-400:400);
y = (1/sqrt(2*pi*s2))*exp(-0.5*(x.*x)/s2);
plot(x+m,y)
binw = s/5;
bins = m + binw*(-20:20);
[NN, XX] = hist (Y, bins);
[xb,yb] = bar(XX,NN);
plot(xb,yb,"-b", x+m,(N*binw)*y,"-g")

Hope this is useful.
Ted.

--------------------------------------------------------------------
Fax-to-email: +44 (0)870 167 1972
Date: 29-Jan-03                                       Time: 17:46:16
------------------------------ XFMail ------------------------------

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```