# Producing the following figure (Containing Normal Distribution and its rotated verion) with tikz

I am trying to produce the following figure with tikz.

This is what I got so far

with the following MWE:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}

\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}

\begin{tikzpicture}
\begin{axis}[
no markers
, domain=-7.5:25.5
, samples=100
, ymin=0
, axis lines*=left
, xlabel=
, every axis y label/.style={at=(current axis.above origin),anchor=south}
, every axis x label/.style={at=(current axis.right of origin),anchor=west}
, height=5cm
, width=20cm
, xtick=\empty
, ytick=\empty
, enlargelimits=false
, clip=false
, axis on top
, grid = major
, hide y axis
, hide x axis
]

\draw[thick, ->] (axis cs: 0, 0) -- (axis cs: 8, 0) node[anchor = north west] {};
\draw[thick, ->] (axis cs: 0, 0) -- (axis cs: 0, 2) node[anchor = south east] {y};

% Normal Distribution 1
\addplot[blue, ultra thick,restrict x to domain=1:5] {gauss(x, 3, 1)};
\pgfmathsetmacro\valueA{gauss(0, 3, 1)}
\draw [dashed, thick, blue] (axis cs:0, 0) -- (axis cs:0, \valueA);
\node[below] at (axis cs:0, -0.02)  {\Large \textcolor{blue}{$Z$}};
\draw[thick, blue] (axis cs:-0.0, -0.01) -- (axis cs:0.0, 0.01);

% Normal Distribution 2
\addplot[rotate=90, green, ultra thick,restrict x to domain=-2:6] {gauss(x, 2, 1)};

% Normal Distribution 3
\addplot[rotate=90, red, ultra thick,restrict x to domain=0:8] {gauss(x, 4, 1)};

\end{axis}

\end{tikzpicture}

\end{document}


Any help to get the required graph will be highly appreciated. Thanks in advance.

• Is it just a representative drawing or do you actually need it to show perfectly the behavior of the normal distribution? If it's just a representation you can easily draw those with controls... – Guilherme Zanotelli Nov 22 '16 at 12:45
• Need to show perfectly the behavior of the normal distribution. – MYaseen208 Nov 22 '16 at 12:49
• Well, than you are in the right track :D. Thogh I think you'd be better of with using just tikz than using pgfplots because you'll need to tweak a lot with the plot. It would be nice if you could also specify what are you having trouble doing, I'm sure there are several questions here that can already help you go further with your drawing. – Guilherme Zanotelli Nov 22 '16 at 14:00
• What you could do is use a different axis environment for each curve using scopes, then combine them outside the axis (to avoid being cropped). – John Kormylo Nov 22 '16 at 15:34

Code is perhaps a bit of a mess. I used two axis environments, the second one rotated. The height of the first is equal to the width of the second. Added coordinates along the plots, and used those to draw the dashed lines after the second axis.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\begin{document}

\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}

\begin{tikzpicture}
\begin{axis}[
name=axis1,
no markers
, domain=0:10,ylabel=$y$,
, samples=200
, ymin=0,ymax=2,xmin=0,xmax=6,
, axis lines*=left
, xlabel=
, every axis y label/.style={at=(current axis.above origin),anchor=south}
, every axis x label/.style={at=(current axis.right of origin),anchor=west}
, height=10cm
, width=8cm,scale only axis,
, xtick=3,xticklabels={$z$},
, ytick=\empty
, enlargelimits=false
, axis on top
]

% Normal Distribution 1
\addplot[blue, ultra thick,restrict x to domain=0.5:5.5] {gauss(x, 3, 1)}
coordinate [pos=0.02] (b1)
coordinate [pos=0.98] (b2)
coordinate [pos=0.7] (bm);

\pgfmathsetmacro\valueA{gauss(0, 3, 1)}

\end{axis}
% Normal Distribution 2
\begin{axis}[
clip=false,
samples=100,
ymin=0,
xmin=-6,xmax=9,
scale only axis,
domain=-2:10,
rotate=90,
at={(axis1.south west)},
hide axis,
anchor=south east,
width=10cm,
height=4cm]

\addplot[green, ultra thick,restrict x to domain=-2:6] {gauss(x, 2, 1)}
coordinate [pos=0] (g1)
coordinate [pos=1] (g2)
coordinate [pos=0.5] (gm);

% Normal Distribution 3
\addplot[red, ultra thick,restrict x to domain=0:8] {gauss(x, 4, 1)}
coordinate [pos=0] (r1)
coordinate [pos=1] (r2)
coordinate [pos=0.5] (rm);
\end{axis}

\draw [very thick] (g1-|b1) -- (g2-|b2);
\draw [very thick] (r1-|b1) -- (r2-|b2);

\draw [dashed] (r1) -| (g1-|b1);
\draw [dashed] (g1) -| (b1);

\draw [dashed] (r2) -| (g2-|b2) node[pos=0.5,right] {$x=+$};
\draw [dashed] (g2) -| (b2) node[pos=0.5,right] {$x=-$};

\draw [shorten <=1mm,Stealth-,thick] (bm) -- ++(3mm,4mm)
node[above,align=center]
{Natural\\variability\\ in $z$};

\path (rm) ++(3mm,2cm) node[align=right] (var) {Variability in $y$\\transmitted\\from$z$};
\draw [-Stealth] (var.west) ++(1.5em,-1ex) -- (gm);
\draw [-Stealth] (var.west) ++(1.5em,-1ex) -- (rm);
\end{tikzpicture}

\end{document}

• Thanks @Torbjon for nice and helpful answer. The blue distribution is little off the axis and not touching the axis. Any thoughts. Thanks – MYaseen208 Nov 23 '16 at 3:59
• @MYaseen208 That is because the end points of the domain of the blue plot are too close to the mean. If you modify restrict x to domain=0.5:5.5 to something like restrict x to domain=0.3:5.7 it will end closer to the axis. Modify as you prefer. – Torbjørn T. Nov 23 '16 at 4:15