# How to plot a multivariate normal distribution?

**URL:** <https://discourse.julialang.org/t/how-to-plot-a-multivariate-normal-distribution/35486>\
**Category:** Visualization\
**Created:** [March 3, 2020, 10:04pm UTC](https://discourse.julialang.org/t/how-to-plot-a-multivariate-normal-distribution/35486 "2020-03-03T22:04:03Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Robert\_Moss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robert_moss/32/48004_2.png) [@Robert\_Moss](https://discourse.julialang.org/u/Robert_Moss)\
**Post date:** [February 23, 2021, 11:37pm UTC](https://discourse.julialang.org/t/how-to-plot-a-multivariate-normal-distribution/35486/7 "2021-02-23T23:37:34Z")

</div>

Just a **warning/note** about x-y ordering when creating the `Z` grid: if you’re plotting as a contour/heatmap, then you’ll want to reverse the `for x in X, y in Y` ordering to be `for y in Y, x in X` so that the grid maps to the x-y axes correctly. This is subtle, as it will not always present itself as a problem depending on the parameters of the `MvNormal`.

Here’s an example plotting a bivariate normal distribution as filled contours:

```julia
using Distributions
using Plots

μ = [0, 0]
Σ = [1 0.9;
     0.9 10]
p = MvNormal(μ, Σ)

X = range(-8, 8, length=100)
Y = range(-8, 8, length=100)
Z = [pdf(p, [x,y]) for y in Y, x in X] # Note x-y "for" ordering
contourf(X, Y, Z, color=:viridis)

```

**To avoid `Z` altogether** , you can pass a function `f` to handle calculating the z-values:

```julia
f(x,y) = pdf(p, [x,y])
contourf(X, Y, f, color=:viridis)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/b/0/b067d60d9b8474c9a1df3e28aa6b9c4a8282e48a.png)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-plot-a-multivariate-normal-distribution/35486)._
