# Draw a grid of bivariate normal distributions in TikZ

This question was answered in this post, but I would like a small adaptation of it.

I would like to take the top solution of the above post. Instead of a single bi-variate distribution, I would like to have a grid of such distributions. The grid should be rectangular with controllable positions for each source.

I would then like to adapt this resulting image to remove all of the axes to leave behind just the surface distribution.

The grid I would like should be something like that here. But the position of each circle should be a 2D Gaussian.

## Desired output

Something like this, with and without projections on the axes.

## The distribution

Each source on the grid should have the following distribution:

$$f(x, y) = \frac{1}{2\pi \sigma_x \sigma_y}\exp[-\frac{(x-\mu_x)^2}{\sigma_x^2} + -\frac{(y-\mu_y)^2}{\sigma_y^2}]$$.

with $\sigma_x = \sigma_y$ and varying values for $\mu_x$ and $\mu_y$ for the different sources. Note that this distribution has a diagonal form for the covariance matrix with elements $\sigma_x$ and $\sigma_y$ on the diagonal.

• And, where is your problem? What have you done so far? – Huang_d Jun 16 '17 at 15:28
• @Huang_d, getting a second distribution using the \addplot3 [surf] plot at a different location. It does not produce 2 seperate distributions. – Sid Jun 16 '17 at 15:31
• One big problem (if I understand correctly) is, you won't be able to draw several intersecting surfaces using PgfPlots, as it won't be able to decide correctly which is in the front / in the back. – marsupilam Jun 16 '17 at 15:31
• @marsupilam, I see. Is there an alternative way to achieve this? – Sid Jun 16 '17 at 15:41
• You could draw each surface only on the relevant domain and put them side by side. Do you have a model picture of what you would like, or more details of which gaussians you want and what grid you are thinking of ? – marsupilam Jun 16 '17 at 15:46

I wonder if I'll ever understand the thing with loops in pgfplots.

## The (simplistic) code

@Jake is right in pointing out this is a lot of computations for pgfplots, and you may be well-advised to pre-compute the functions via an external tool

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
[
declare function=
{
gaussian(\x)= exp(-.5*\x^2);
gaussianDouble(\x,\y)= gaussian(\x)*gaussian(\y);
}
]
\begin{axis}
\foreach\i in {0,4,...,12}
{
\foreach\j in {0,4,...,12}
{
}
}
\end{axis}
\end{tikzpicture}
\end{document}


## In Scilab

Not an answer, but is this what you want ?

If so, may I edit your question to include the pic in it ?

### The (Scilab) code

function z=normal(x,y)
norm = x.^2 + y.^2
z = exp(-.5*norm)
endfunction
clf()
x=linspace(-3,3,30)
[X,Y]=meshgrid(x)
for i=0:2:10
for j=0:2:10
surf(X+i,Y+j,normal(X,Y))
end
end
f=gcf()
f.color_map = autumncolormap(32);

• Yes, this is what I would like thanks - with and without a distribution shown projected on the xz and yz axes (much like the original post in question). I would still prefer it in TikZ, but you are most welcome to edit the question to illustrate the desired output. – Sid Jun 16 '17 at 16:27
• @Sid Alright, then, you would need to tell us the x and y step of the lattice you are using, and the covariance matrix for the Gaussian distributions. Otherwise, no computation can be done... – marsupilam Jun 16 '17 at 16:31
• Added the form of the distribution to the question. – Sid Jun 16 '17 at 17:00
• It's true that it takes 20-30 seconds in pgfplots, but remember that you only have to compile it once. You can then subsequently include it using \includegraphics. – JPi Jun 16 '17 at 18:30
• @JPi Sure, but I chose a rather low samples value and few bumps... It's unlikely you can get stellar graphics using this simple approach. – marsupilam Jun 16 '17 at 18:37