# Visualization of random distribution with 3 variables

Suppose I have a joint distribution of three random variables $x,y,z$, $P(x,y,z)$. For simplicity, let's suppose those three rvs. are discrete. The distribution will be represented in Python as a 3-dimensional numpy array. In the cases where there are only 2 rvs., we can plot a surface plot, but in 3 rvs. case, I cannot think of a good way to do it.

My question is, is there a way to visualize $P(x,y,z)$ (preferably in Python)?

• You can always plot the three marginals $P(x,y)$, $P(y,z)$ and $P(z,x)$. If one of the three rv's has a small number of values, say $X$ with values in $\{1,\ldots,k\}$, you can also plot the slices $P(i,y,z)$ for $1\le i\le k$. – Xi'an Dec 28 '14 at 8:56
• I think that you could map one of the variables' values onto color range, thus, producing dynamically-colored surface plots (the term is mine). For a simpler alternative, if applicable, see this. – Aleksandr Blekh Dec 28 '14 at 9:02
• Check this presentation on multi-domensional visualizations, you might be interested in 3D functions towards the end of the slides. – Aksakal Dec 31 '14 at 23:03

You might want to try a ternary plot. Unfortunately, I'm not sure how to code one in Python, but here's an R version, based on the help for ternaryplot in the vcd package. (You can also try the ggtern package, which uses ggplot2 to create ternary plots):

library(vcd)
data("Hitters")
colors <- c("black","red","green","blue","red","black","blue")
pch <- substr(levels(Hitters$Positions), 1, 1) ternaryplot( Hitters[,2:4], pch = as.character(Hitters$Positions),
col = colors[as.numeric(Hitters$Positions)], main = "Baseball Hitters Data" ) grid_legend(0.8, 0.9, pch, colors, levels(Hitters$Positions),
title = "POSITION(S)")


As you can see, the plot shows each baseball player's relative performance on three variables--Errors, Putouts, and Assists. In this case, the plot also uses symbols to show each player's field position.

• I don't think a ternary plot is applicable here. Ternary plot requires the three variables sum to a constant value and only shows the proportions. How would it apply to an arbitrary distribution of 3 independent variables? – xan Dec 29 '14 at 3:43
• As xan said, ternary plot only applies to the case where $x+y+z$ is some constant. This is not what I needed. – wdg Dec 29 '14 at 14:35

You could try to create a dynamic plot, like this one whit shiny. But instead of the user selecting the number of points (as in the example of shiny) you could select the value of the rv that has the smaller number of values.

In other words (from comment), plot a series of surface plots (or 2D density plots) of P(X,Y,Z=const).

• I don't understand what you meant. – wdg Dec 29 '14 at 7:47
• You can have as many surface plots of the rv X, Y as values of Z. – Daniel Salgado Olvera Dec 29 '14 at 8:06
• Did you mean fixing the value of Z, and plot a series of surface plots of P(X,Y,Z=const)? – wdg Dec 29 '14 at 14:38
• Yes, that's what I mean. – Daniel Salgado Olvera Dec 29 '14 at 21:55