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)?

  • 1
    $\begingroup$ 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$. $\endgroup$
    – Xi'an
    Dec 28 '14 at 8:56
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 28 '14 at 9:02
  • $\begingroup$ Check this presentation on multi-domensional visualizations, you might be interested in 3D functions towards the end of the slides. $\endgroup$
    – 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):

colors <- c("black","red","green","blue","red","black","blue")
pch <- substr(levels(Hitters$Positions), 1, 1)
  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.

enter image description here

  • $\begingroup$ 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? $\endgroup$
    – xan
    Dec 29 '14 at 3:43
  • $\begingroup$ As xan said, ternary plot only applies to the case where $x+y+z$ is some constant. This is not what I needed. $\endgroup$
    – 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).

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.