Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Gauss Shape in gnuplot

6,974 views
Skip to first unread message

Randa

unread,
Sep 8, 2010, 1:30:00 PM9/8/10
to
Hi,

I'm trying to plot a Gaussian (bell) shape in gnuplot. I am using the
function norm(x). The problem is that norm(x) plots just the first
part of the Gaussian bell shape, I don't know how to make the line
"come back down"... I used "help norm", but didn't get much
information either.
I feel like this is something trivial and simple, but I can't figure
it out...

Any help would be highly appreciated.

Karl

unread,
Sep 8, 2010, 1:46:28 PM9/8/10
to
norm(x) is not a gaussian, but the integral.

help norm

says "returns the CUMULATIVE normal (Gaussian)distribution function"
(emphasis added by me)

just plot your favourite variation of

gauss(x) = exp(-(x/t)**2)

Karl

Brendan Halpin

unread,
Sep 8, 2010, 1:49:15 PM9/8/10
to
norm(x) plots the cumulative probability distribution. You want the
density function. I can't see a built-in command for that but you can
define your own as follows:

normal(x, mu, sd) = (1/(sd*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sd**2))

Then

plot [-3:3] normal(x,0,1)

will show you the standard normal.

Brendan
--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147
mailto:brendan...@ul.ie http://www.ul.ie/sociology/brendan.halpin.html

Randa

unread,
Sep 8, 2010, 2:29:48 PM9/8/10
to
On Sep 8, 1:49 pm, brendan.hal...@ul.ie (Brendan Halpin) wrote:
> norm(x) plots the cumulative probability distribution. You want the
> density function. I can't see a built-in command for that but you can
> define your own as follows:
>
> normal(x, mu, sd) = (1/(sd*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sd**2))
>
> Then
>
> plot [-3:3] normal(x,0,1)
>
> will show you the standard normal.
>
> Brendan
> --
> Brendan Halpin,  Department of Sociology,  University of Limerick,  Ireland
> Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147
> mailto:brendan.hal...@ul.ie  http://www.ul.ie/sociology/brendan.halpin.html

Thanks for your reply. I got the shape, but I am trying to constrain
it in the range:

set xrange [2.2:-2.2]
set yrange [0.01:0.15]

Instead of plot [-3:3] normal(x,0,1) , I used plot [-2.2:2.2]
normal(x,0.01,0.15) ... but it didn't cover the range I wanted... Am I
doing something wrong?

Brendan Halpin

unread,
Sep 8, 2010, 5:34:09 PM9/8/10
to
On Wed, Sep 08 2010, Randa wrote:

> Thanks for your reply. I got the shape, but I am trying to constrain
> it in the range:
>
> set xrange [2.2:-2.2]
> set yrange [0.01:0.15]
>
> Instead of plot [-3:3] normal(x,0,1) , I used plot [-2.2:2.2]
> normal(x,0.01,0.15) ... but it didn't cover the range I wanted... Am I
> doing something wrong?

normal() takes 3 parameters, x, the mean, and the standard deviation.
Thus normal(x,0,1) plots the standard normal distribution.
normal(x,100,10) plots a normal distribution with mean 100 and standard
deviation 10.

The standard deviation affects how wide and therefore how tall the
distribution is at its peak. For a standard deviation of 1 the peak is
about 0.4.

Brendan
--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147

mailto:brendan...@ul.ie http://www.ul.ie/sociology/brendan.halpin.html

Dan Luecking

unread,
Sep 8, 2010, 5:56:11 PM9/8/10
to
On Wed, 8 Sep 2010 11:29:48 -0700 (PDT), Randa <asa...@mail.uc.edu>
wrote:

If the value of x is more than 0.6 or less than -0.6, the
value of normal(x,0.01,0.15) is less than .001. This is
about 0.03% of the height of the graph. On my screen, that
means it is 0 when the pixel resolution of the display is
taken into account.

Thus the graph is present all the way from -2.2 to 2.2, but
it is covered up by the bottom border of the graph at y=0,
except in a range of about [-0.6:0.6].

Set the y range to include some negative values and you will
see the whole thing:

plot [-2.2:2.2] [-0.05:3.0] normal(x,0.01,0.15)


Dan
To reply by email, change LookInSig to luecking

Randa

unread,
Sep 8, 2010, 6:55:25 PM9/8/10
to
This might be a stupid question, but if I divide the whole equation (1/
(sd*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sd**2)) by 3.6 for example to fit
my make the shape around my data,does it still a valid Gauss
distribution, with the same mean and standard deviation?

Thanks!

Ingo Thies

unread,
Sep 9, 2010, 3:09:23 AM9/9/10
to

If the integral of your function is approx. 1/3.6, then yes (e.g. if you
are plotting a distribution that is not normalized to 1 but so 1/3.6).

The standard deviation is related to the actual integral of your
Gaussian (the well-known 68% inclusion in the case of symmetric
1-sigma-limits), not necessarily to 1. The mean of a Gaussian is just
the position of the peak (this may not be valid for non-Gaussian
distributions), and therefore not depending on the y-scaling.


Ingo

Randa

unread,
Sep 12, 2010, 8:41:08 PM9/12/10
to
But how do I know if the integral of my function is approx. 1/3.6 (How
do I know if the distribution that is normalized to 1/3.6).?
Thanks!

Randa

unread,
Sep 15, 2010, 11:20:02 PM9/15/10
to
> mailto:brendan.hal...@ul.ie  http://www.ul.ie/sociology/brendan.halpin.html

Is there anyway I can add the skewness to this equation? So that the
curve can better fit my data?

Brendan Halpin

unread,
Sep 16, 2010, 4:22:04 AM9/16/10
to
On Thu, Sep 16 2010, Randa wrote:

> Is there anyway I can add the skewness to this equation? So that the
> curve can better fit my data?

Yes. But you really seem to be interested in statistical issues, rather
than simply plotting. If you want to go further with this, you ought to
investigate statistics software.

This link:
http://cran.r-project.org/doc/contrib/Ricci-distributions-en.pdf
leads to a nice discussion of how to fit data to distributions using R.

Brendan
--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147

mailto:brendan...@ul.ie http://www.ul.ie/sociology/brendan.halpin.html

Ingo Thies

unread,
Sep 18, 2010, 6:14:20 AM9/18/10
to
Am 2010-09-13 02:41, schrieb Randa:
> But how do I know if the integral of my function is approx. 1/3.6 (How
> do I know if the distribution that is normalized to 1/3.6).?
> Thanks!

Sorry for late reply.

If the Gaussian shape is always preserved (no distortion etc.), than you
can derive the normalization simply by the x- and y-scale. I.e., if the
maximum of the function is 0.399 (exact: 1/sqrt(2*pi)) but the curve is
stretched twice (i.e. sigma=2), than the integral and thus the
normalization factor is 2. The same would be true for sigma=1 but a
maximum = 0.798.

In short: x-stretch times y-stretch (all with respect to the standard
Gaussian function with sigma=1) is the normalization factor by which the
function has to be devided to get the integral=1.

Ingo

Randa

unread,
Sep 27, 2010, 5:41:56 AM9/27/10
to
Thank you all for all your replies...
I've been reading and looking and asking about this, but I still don't
know how to solve it...

As some of you suggested, it appears that my data is not Gaussian.
Maybe I need to try other functions?? So if I want to check other
functions do I have to use the equation of each distribution and
check? Or is there a way that finds the distribution according to my
data?
I will separately include first my data, then the norm I created form
using the mean and standard deviation of my sample, and finally the
norm created by simply fitting the data:

[CODE]
-2.1 0.037037
-1.8 0.037037
-1.6 0.037037
-1.4 0.111111
-1.2 0.037037
-1.1 0.111111
-1.0 0.074074
-0.9 0.222222
-0.8 0.037037
-0.7 0.074074
-0.5 0.111111
-0.4 0.148148
-0.3 0.037037
-0.2 0.296296
-0.1 0.222222
0.0 0.185185
0.1 0.407407
0.2 0.111111
0.3 0.222222
0.4 0.185185
0.5 0.111111
0.6 0.148148
0.7 0.037037
0.8 0.037037
0.9 0.074074
1.0 0.037037
1.8 0.037037
[/CODE]

[CODE]
set boxwidth 0.1
set xrange [2.2:-2.2]


normal(x, mu, sd) = (1/(sd*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sd**2))

plot normal(x,-0.25,0.95), "data.txt" using 1:2 w boxes

[/CODE]

[CODE]
set boxwidth 0.1
set xrange [2.2:-2.2]
f(x)= (1/(a*sqrt(2*pi)))*exp(-(x-b)**2/(2*a**2))
fit f(x) "check1234.txt" using 1:2 via a,b


normal(x, mu, sd) = (1/(sd*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sd**2))

plot "data.txt" using 1:2 w boxes, f(x)
[/CODE]

Christoph Bersch

unread,
Sep 27, 2010, 7:52:53 AM9/27/10
to
Hi,

Randa wrote:
>
> As some of you suggested, it appears that my data is not Gaussian.
> Maybe I need to try other functions?? So if I want to check other
> functions do I have to use the equation of each distribution and
> check? Or is there a way that finds the distribution according to my
> data?

well, usually the distribution should depend on the experiment or source
where you got your data from and the model associated with it.
You cannot use any distribution just to get a nice-looking curve.

Christoph

Randa

unread,
Sep 27, 2010, 3:57:57 PM9/27/10
to
This is a research project, so it's not something done before, I am
just studying a set of data I got trying to figure out if it follows a
certain statistical distribution.

On Sep 27, 7:52 am, Christoph Bersch <use...@bersch.net> wrote:
> Hi,

Randa

unread,
Oct 1, 2010, 8:48:45 AM10/1/10
to


Any suggestions?

Brendan Halpin

unread,
Oct 1, 2010, 9:15:54 AM10/1/10
to
On Fri, Oct 01 2010, Randa wrote:

> On Sep 27, 3:57 pm, Randa <asa...@mail.uc.edu> wrote:
>> This is a research project, so it's not something done before, I am
>> just studying a set of data I got trying to figure out if it follows a
>> certain statistical distribution.

> Any suggestions?

I'm not really clear what your data is. Is it a distribution? The plot
you show makes it look like a histogram, but in the data the second
column (which looks like a probability density or proportional
distribution) sums to about 3.2, so it is doesn't look like a relative
distribution.

If your data isn't in the form of a distribution, why compare it with a
standard distribution?

In the form you showed your data, if it were a distribution I would
interpret column 2 as the number of cases (or proportion of cases)
falling within a band centred on the column 1 value (where the with of
the band is the gap between successive values; I note your values are
not all the same distance apart).

Randa

unread,
Oct 1, 2010, 4:49:12 PM10/1/10
to
> mailto:brendan.hal...@ul.ie  http://www.ul.ie/sociology/brendan.halpin.html

Yes! You're right!! This is what it is:


"In the form you showed your data, if it were a distribution I would
interpret column 2 as the number of cases (or proportion of cases)
falling within a band centred on the column 1 value (where the with
of
the band is the gap between successive values";

But, actually the values are all the same values apart... I mean they
all have the same width, but some values are missing because the
number of cases there is zero.

Brendan Halpin

unread,
Oct 1, 2010, 5:45:13 PM10/1/10
to
OK, I see.

Your density figure is wrong -- it looks like it is calculated with a
sample size of 27 whereas it is really 86 (or multiples thereof). Also
your mean and sd are wrong, because they seem to be calculated on the x
values without taking into account multiple instances of them.

On 86 observations, the mean is -.1616279, standard deviation .6860232.

If you re-define your density figures to sum to 1.0, it will work:

plot normal(x,-0.1616279,0.6860232), "x.dat" using 1:($2*86/27) w boxes

Brendan

PS: your data set is far too small for a real test of normality.

--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147

mailto:brendan...@ul.ie http://www.ul.ie/sociology/brendan.halpin.html

Randa

unread,
Oct 2, 2010, 1:56:59 PM10/2/10
to
Yes! You are right the actual sample size is 86. If you notice in one
of my earlier posts the data.txt file contains : 1:($2*86/27)
But still when I use the numbers you provided (the mean is  -.1616279,
standard deviation .6860232) I don't get a good fit. Did you tried it
and it worked?

Thanks

> mailto:brendan.hal...@ul.ie  http://www.ul.ie/sociology/brendan.halpin.html

Brendan Halpin

unread,
Oct 2, 2010, 4:20:27 PM10/2/10
to
On Sat, Oct 02 2010, Randa wrote:

> But still when I use the numbers you provided (the mean is  -.1616279,
> standard deviation .6860232) I don't get a good fit. Did you tried it
> and it worked?

Using gnuplot and this code:

reset
set boxwidth 0.1
set xrange [-2.2:2.2]


normal(x, mu, sd) = (1/(sd*sqrt(2*pi)))*exp(-(x-mu)**2/(2*sd**2))

plot normal(x,-0.1616279,0.6860232), "x.dat" using 1:($2*86/27) w boxes

I get this result:
http://teaching.sociology.ul.ie/bhalpin/randa_gnuplot.pdf

Putting your data into another program (Stata), and recreating the
individual observations, I get this:
http://teaching.sociology.ul.ie/bhalpin/randa_stata.pdf

Stata chooses to create wider bins which reduce some of the jaggedness
of the gnuplot histogram, which is helpful, but even then the histogram
only loosely approximates the normal shape. However, as I said before
this is a very small data set so even if it was from a strictly normal
population it likely wouldn't look very like it.


Regards,

Brendan


--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-338562; Room F1-009 x 3147

mailto:brendan...@ul.ie http://www.ul.ie/sociology/brendan.halpin.html

0 new messages