TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can you please help me with making a 2D Guassian sample with specified means and variances?

I only know how to make a guassian curve :D

\usepackage{paralist,pst-func, pst-plot, pst-math, pstricks-add,pgfplots}
    \begin{axis}[hide axis,clip=false,xmin=-4,xmax=4,xlabel={X},ymin=0,ymax=1] 
    \addplot[color=lime, samples=100] {1/sqrt(2)*exp(-(x+1)^2/1)} ;

And here is the formula for it: enter image description here

And this would be an example of three normal distributions together: enter image description here

share|improve this question
Since this is not a math question site, you should supply the math related to what you want to graph, and explain what parts you are having difficulty with. – Peter Grill Apr 9 '13 at 18:15
Hi Naji. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. – Claudio Fiandrino Apr 9 '13 at 18:20
formula is on wikipedia: en.wikipedia.org/wiki/Normal_distribution – long tom Apr 9 '13 at 18:21
@PeterGrill, well I thought the normal distribution is famous enough to not to express it here. – Naji Apr 9 '13 at 18:22
up vote 7 down vote accepted

For this, you need a way to generate normally distributed random numbers. One way of doing this is to use the Box-Muller transform.

Here's an example of using PGFPlots for this (based on my answer to TikZ: Drawing the same data with scatter plots and parallel coordinates). I've plotted the marginal distributions to show that the numbers are indeed approximately normal:

% Create a function for generating inverse normally distributed numbers using the Box–Muller transform
    axis equal image,
    ymin=-2.5, ymax=2.5,
    extra x ticks={-2,...,2},
    every extra x tick/.style={
        tick align=outside,
    extra y ticks={-2,...,2},
    every extra y tick/.style={
        tick align=outside,
\addplot [only marks, samples=100] ({invgauss(rnd,rnd)},{invgauss(rnd,rnd)});
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.