# Could TiKz make a picture of a matrix with pictorial normal distributions?

Is TikZ the best way of doing this do you think?

Any other method you can think of?

• Depending on the need for precision for the red lines, this can be done by TikZ \matrix very easily. Or you use a math-mode aligned environment and place little TikZ images in there. Commented Jul 2, 2013 at 21:39
• Hmm at the very least like the picture shown so no particular precision, as long as I can get them to follow some normal distribution curve shape. Is this the package you'd use?
– HCAI
Commented Jul 2, 2013 at 21:41
• Are those identical normal distributions? If not, what are the parameters that define those distributions? Commented Jul 2, 2013 at 22:28
• Commented Jul 3, 2013 at 1:20

If you do not want that much control and precision over and in your little pictures, you can use the following solution.

It uses

The TikZ picture consists of two paths for the axes and one path for the plot.

The picture’s usual coordinate system is shrinked by the factor 10. I took a rather unorthodox way for the axes and just used coordinates of the factor 1 (base unit). The actual x and the y value are computated via the x and y factor.

This way, the axes do not scale with the plot or the total TikZ picture but after all the domain setting also affect the plot. A special style my be introduced here that sets all other settings based on the domain. I have used two examples with borderline values so that either the y axis had to be extended or the whole picture has to be shrunken which probably won’t be a real use-case as this distorts any relation to the other pictures.

Be aware of a high samples value because this slows down the process very much. It may be advisavle to externalize the calculation (gnuplot) and/or use the external library of TikZ. You also may pre-calculate some values (you’ll need a table plot then) and simply change the coordinate system and/or scale the resulting picture.

## Code

\documentclass{article}
\usepackage{amstext}
\usepackage{blkarray}
\usepackage{tikz}
\tikzset{
declare function={NormDist(\xValue,\meanValue,\standardDeviation)=1/(\standardDeviation*sqrt(2*pi))*exp(-((\xValue-\meanValue)^2)/(2*\standardDeviation^2));},
}
\newcommand*{\tikzNormDist}[3][]{%
\begin{tikzpicture}[every mini plot picture/.try,#1]
\path[every mini plot x axis/.try] (-1,0) -- (1,0);
\path[every mini plot y axis/.try] (0,0) -- (0,1) [mini plot y axis arrow/.try];
\draw[every mini plot plot/.try]   plot[variable=\x] (\x,{NormDist(\x,#2,#3)});
\end{tikzpicture}
}
\tikzset{
every mini plot picture/.style={x=+1mm, y=+1mm, baseline=.5},
every mini plot x axis/.style={draw,thin,x=+2mm},
every mini plot y axis/.style={draw,thin,y=+2.2mm},
every mini plot plot/.style={
thick,
red,
smooth,
samples=40,
domain=-2:2,
},
mini plot y axis arrow/.style={
insert path={
[line cap=round, line join=round]+(+.25mm,+-.5mm) -- +(+0,+0) -- +(+-.25mm,+-.5mm)
}
}
}
\begin{document}
$\begin{blockarray}{l c*2{@{\hphantom{{}+{}}}c}} & \text{Table} & \text{Bed} & \text{Pat} \\ \begin{block}{l(*2{c@{{}+{}}}c)} \text{Table} & \tikzNormDist{0}{0.3} & \tikzNormDist[ every mini plot plot/.append style={blue!50!red,fill=blue!50!green}, every mini plot y axis/.append style={y=+3.5mm} ]{-.3}{0.15} & \tikzNormDist{-.2}{0.4} \\ \text{Bed} & \tikzNormDist{.2}{0.5} & \tikzNormDist{.7} {0.7} & \tikzNormDist{-.5}{0.7} \\ \text{Pat} & \tikzNormDist{-.1}{0.4} & \tikzNormDist{-.1}{1} & \tikzNormDist[ every mini plot plot/.append style={domain=-10:10,x=+.25mm,y=+5mm} ]{0}{2} \\ \end{block} & {} = 1 & {} = 1 & {} = 1 \\ \end{blockarray}$
\end{document}


## Output

• This is amazing! Definitely giving bounty for this when it's available! Thank you so much! :-)
– HCAI
Commented Jul 3, 2013 at 18:00

Since I am not to experienced with latex and know tikz I would use it. If you happen to be familiar with R you can combine the two (e.g.http://www.texample.net/tikz/examples/tikzdevice-demo/).

Another example what you can do with tikz regarding normal distributions is this: http://www.texample.net/tikz/examples/animated-distributions/.

I can not quite make sense of the matrix you drew up. When using a matrix node to organize multiple subpictures you might have to wrap the subfigures in a matrix like this: {\tikz{....}} (see https://tex.stackexchange.com/a/119306/19326).

You could consider using a picture for the normal distributions and use tikz overlay mode. On semi alternative would be pgfplots (which is also based on tikz, but why learn a new tool), another one would be pstricks.

• PGFPlots is based on TikZ/PGF, not PSTricks. It makes the creation of plots a whole lot easier than relying only on the built-in TikZ \draw plot command, because it takes care of the scaling, the axes, the legends, the labels, basically all the things that would be very tedious to build by hand in TikZ. It also allows you to plot datafiles and create 3D plots of functions and data. If you need that kind of functionality, it makes sense to learn how to use PGFPlots (if you know how to use TikZ, it's a very small step to PGFPlots).
– Jake
Commented Jul 3, 2013 at 1:32
• @Jake: point taken, I got carried away and formulated the pstricks part unclear, which I wanted to mention as a nother alternative. As for the PGFPlots part, will it be integrated into tikz (there is datavisualization in tikz development) or will it remain separate?
– ted
Commented Jul 3, 2013 at 6:32