Is there a package in R or a library in Python with some functions that allow a graphical representation of a multivariate (2 - dimensional) Gaussian mixture as a joint distribution? By this I mean that I'm not interested in the marginal distributions.

I'm not doing any sort of estimation or approximation, I would just like to visualize the pdf.

By 2-dimensional, I mean that the mixture distribution is a mixture of 2-D normal random variables.

As an example, I would expect something like this, possibly smoother where the two high-density areas meet

enter image description here

  • I don't understand the question. On the Wikipedia page for Mixture Models, could you find the kind of plot (or visualization/graphical representation) you want to create? Then please update your question with a reference to that plot (e.g. by mentioning its caption). Thanks. – knb Jul 7 '19 at 8:11
  • I guess that the 2-D part might be confusing. I will update the question – Easymode44 Jul 7 '19 at 11:26
  • In python you can use matplotlib for surface plots: matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#surface-plots – jitter Jul 7 '19 at 13:33

In R you can use the ggplot2 package. Some functions to do 2D density plots are built-in.

However, it is good visualization practice not to rely on 3D graphics for quantitative purposes (see , for example, Kieran Healy's Book Data Visualization Ch 1), because human perception is easily fooled with respect to depth effects, interaction of colors, and readability of axes labels and grids in 3D space.

So you could plot the built-in dataset "faithful" (showing a bimodal distribution of geyser eruptions who are either short and occuring in quick succession, or big and occuring more rarely);

library(ggplot2)      # plotting library
library(viridis)      # use viridis color palette (blue -> yellow)
theme_set(theme_bw()) # white background

# create an "isolines" plot of the data values
ggplot(faithful, aes(x = waiting, y = eruptions)) +
  geom_density_2d() +
        labs(title = "Old Faithful: Geyser Eruptions",
             subtitle = "Isolines plot",
             x ="Waiting times (minutes)",
             y = "Number of Eruptions")



# create a plot of the density values which ggplot calculated internally
# (..density.. column)
ggplot(faithful, aes(x = waiting, y = eruptions)) +
    geom = "tile",
    aes(fill = ..density..),
    contour = FALSE
  ) +
  scale_fill_viridis() +
  labs(title = "Old Faithful: Geyser Eruptions",
       subtitle = "Viridis color scale",
       x ="Waiting times (minutes)",
       y = "Number of Eruptions")


If you really need 3d plots, there are other alternatives. Run, for instance, demo(misc3d::lighting), or check answers to question Plot 3d density on Stackoverflow.

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.