# How can two bivariate normal distributions be plotted in one plot?

I would like to visualize the following two bivariate normal distributions: I found Jakes awesome answer and adjusted it so that the plots get rotated the correct way.

## Current plot ## Question

How can I make the plot better viewable? (E.g. only displaying the function that is currently on top but keeping the correct color?)

## MWE

% Thanks to Jake for the template
% https://tex.stackexchange.com/a/31715/5645
\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
colormap={whitered}{color(0cm)=(white); color(1cm)=(orange!75!red)},
colormap={whiteblue}{color(0cm)=(white); color(1cm)=(blue)},
}

\begin{tikzpicture}[
declare function={mu11=60;},
declare function={mu12=20;},
declare function={sigma11=5;},
declare function={sigma12=5;},
declare function={mu21=70;},
declare function={mu22=40;},
declare function={sigma21=5;},
declare function={sigma22=5;},
declare function={rho=0.8;},
declare function={normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));},
declare function={bivar(\ma,\sa,\mb,\sb,\rho)=
1/(2*pi*\sa*\sb*\rho) * exp(-((x-\ma)^2/\sa^2 + (y-\mb)^2/\sb^2 - (2*\rho*(x-\ma)*(y-\mb))/(\sa*\sb)))/(2*(1-\rho*\rho));}]
\begin{axis}[
width=15cm,
view={-15}{70},
enlargelimits=false,
grid=major,
domain=40:90,
y domain=0:60,
samples=26,
xlabel=$x_1$,
ylabel=$x_2$,
zlabel={$P$},
colorbar,
colorbar style={
at={(1,0)},
anchor=south west,
height=0.25*\pgfkeysvalueof{/pgfplots/parent axis height},
title={$P(x_1,x_2)$}
}
]
\addplot3 [surf, opacity=0.8,fill opacity=0.9,colormap={whitered}{color(0cm)=(white); color(1cm)=(orange!75!red)}, samples=50] {bivar(mu11,sigma11,mu12,sigma12,rho)};
\addplot3 [surf, opacity=0.5,fill opacity=0.3,colormap={whiteblue}{color(0cm)=(white); color(1cm)=(blue)}, samples=50] {bivar(mu21,sigma21,mu22,sigma22,rho)};

\draw [black!50] (axis cs:-1,0,0) -- (axis cs:4,0,0);
\draw [black!50] (axis cs:0,-1,0) -- (axis cs:0,4,0);

\node at (axis cs:-1,1,0.18) [pin=165:$P(x_1)$] {};
\node at (axis cs:1.5,4,0.32) [pin=-15:$P(x_2)$] {};
\end{axis}
\end{tikzpicture}
\end{document}

• Do you mean some thing like this: i.stack.imgur.com/oF9Qf.png ? – user11232 Aug 31 '14 at 1:08
• @HarishKumar I can see only one plot in the link you've posted. – Martin Thoma Aug 31 '14 at 1:18
• The other plot is there but very dim. I don't understand "only displaying the function that is currently on top but keeping the correct color?" (Sorry). Can you please expand on that? – user11232 Aug 31 '14 at 1:20
• @HarishKumar: If you remove the opacity and fill opacity attributes of the plots, you will only see the last plot. The reason seems to be that the plot is put simply over the image. However, I would rather want to see at each (x1,x2) position of the image the plot that has higher value. So around (60, 20) that would be the red plot, but around (70, 40) that would be the blue dot. – Martin Thoma Aug 31 '14 at 1:27

Since PGFPlots can't combine the plots from different \addplot commands, you'll have to use a single \addplot command.

To only show the larger of the two distributions at each point, you can use

\addplot3 [...] {
max(
bivar(mu11,sigma11,mu12,sigma12,rho),
bivar(mu21,sigma21,mu22,sigma22,rho)
)};


To use different colours for the two distributions, you can create a single colormap that runs from blue to white to orange, and map one of the distributions to the negative domain and the other to the positive domain using

\addplot3 [...,
colormap={bluewhitered}{color(0cm)=(blue); color(0.5cm)=(white); color(1cm)=(orange!75!red)},
point meta={
(
bivar(mu11,sigma11,mu12,sigma12,rho)>
bivar(mu21,sigma21,mu22,sigma22,rho)?
bivar(mu11,sigma11,mu12,sigma12,rho):
-bivar(mu21,sigma21,mu22,sigma22,rho)
)
}
] { ... }; \documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
colormap={whitered}{color(0cm)=(white); color(1cm)=(orange!75!red)},
colormap={whiteblue}{color(0cm)=(white); color(1cm)=(blue)},
}

\begin{tikzpicture}[
declare function={mu11=60;},
declare function={mu12=20;},
declare function={sigma11=5;},
declare function={sigma12=5;},
declare function={mu21=70;},
declare function={mu22=40;},
declare function={sigma21=5;},
declare function={sigma22=5;},
declare function={rho=0.8;},
declare function={normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));},
declare function={bivar(\ma,\sa,\mb,\sb,\rho)=
1/(2*pi*\sa*\sb*\rho) * exp(-((x-\ma)^2/\sa^2 + (y-\mb)^2/\sb^2 - (2*\rho*(x-\ma)*(y-\mb))/(\sa*\sb)))/(2*(1-\rho*\rho));}]
\begin{axis}[
width=15cm,
view={-15}{70},
enlargelimits=false,
grid=major,
domain=40:90,
y domain=0:60,
samples=50,
xlabel=$x_1$,
ylabel=$x_2$,
zlabel={$P$},
colorbar,
colorbar style={
at={(1.1,0)},
anchor=south west,
height=0.25*\pgfkeysvalueof{/pgfplots/parent axis height},
title={$P(x_1,x_2)$}
}
]
surf,
colormap={bluewhitered}{color(0cm)=(blue); color(0.5cm)=(white); color(1cm)=(orange!75!red)},
point meta={
(
bivar(mu11,sigma11,mu12,sigma12,rho)>
bivar(mu21,sigma21,mu22,sigma22,rho)?
bivar(mu11,sigma11,mu12,sigma12,rho):
-bivar(mu21,sigma21,mu22,sigma22,rho)
)
}
] {
max(
bivar(mu11,sigma11,mu12,sigma12,rho),
bivar(mu21,sigma21,mu22,sigma22,rho)
)};

\draw [black!50] (axis cs:-1,0,0) -- (axis cs:4,0,0);
\draw [black!50] (axis cs:0,-1,0) -- (axis cs:0,4,0);

\node at (axis cs:-1,1,0.18) [pin=165:$P(x_1)$] {};
\node at (axis cs:1.5,4,0.32) [pin=-15:$P(x_2)$] {};
\end{axis}
\end{tikzpicture}
\end{document}

• Thank you, that looks exactly like what I wanted to get! However, I'm not quite sure if I understand the coloring part. Does point meta assign each point another (real) value to which one colors it? Just to make sure I understand it: This would not work for 3 distributions, would it? – Martin Thoma Aug 31 '14 at 16:10