# Multiple normal distributions in one 3D plot

**URL:** <https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969>\
**Category:** Visualization\
**Tags:** glmakie\
**Created:** [March 12, 2023, 6:08pm UTC](https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969 "2023-03-12T18:08:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mouad](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Mouad](https://discourse.julialang.org/u/Mouad)\
**Post date:** [March 12, 2023, 6:08pm UTC](https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969/1 "2023-03-12T18:08:07Z")

</div>

Hello, I am trying to plot multiple normal distributions centered around the same mean with different variations in GLmakie. Pretty much this picture (generated in wolfram alpha):

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

Hopefully, also animate it by making the mean or the variances observable. Can someone please walk me through a solution?

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [March 13, 2023, 5:44pm UTC](https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969/2 "2023-03-13T17:44:25Z")

</div>

Hi @Mouad .

You can use something like the following:

```julia
using Distributions, GLMakie

μ3 = 0.0;
σ3 = range(1.0,10.0; length=25);
y3 = range(-10.0, 10.0; length=101);

f3 = Figure()
ax3 = Axis3(f3[1,1])
for i in σ3
    d3 = pdf.(Normal(μ3, i), y3);
    lines!(ax3, [i], y3, d3)
end
f3

```

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [March 13, 2023, 6:25pm UTC](https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969/3 "2023-03-13T18:25:34Z")

</div>

Also, to specify colors along a color scale, you could add the following code:

```julia
using ColorSchemes

colors = get(ColorSchemes.viridis, range(0.0,1.0; length=length(σ3)));

```

where you can choose your preferred color scale substituting `viridis` with one of the scale names you find on the [Makie color page](https://docs.makie.org/stable/documentation/colors/index.html#colors).

Then you use add the color to the `lines!` command in the plotting cycle. So, the full working example could become

```julia
using Distributions, GLMakie, ColorSchemes

μ3 = 0.0; 
σ3 = range(1.0,10.0; length=25);
y3 = range(-10.0, 10.0; length=101);
colors = get(ColorSchemes.viridis, range(0.0,1.0; length=length(σ3)));

f3 = Figure()
ax3 = Axis3(f3[1,1])
for (i, σ) in enumerate(σ3)
    d3 = pdf.(Normal(μ3, σ), y3);
    lines!(ax3, [i], y3, d3, color=colors[i])
end
f3

```

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [March 13, 2023, 10:02pm UTC](https://discourse.julialang.org/t/multiple-normal-distributions-in-one-3d-plot/95969/4 "2023-03-13T22:02:13Z")

</div>

To compare the pdfs and to convey the right information on the normal distribution, each normal pdf of mean \mu =0, and standard deviation, σ ∈ 0.5:0.25:4.0, should be represented on the interval (-3σ, 3σ), because if X \sim N(0, σ), then the probability, P(-3σ \< X \< 3\sigma)=\Phi(3)-\Phi(-3)=2\Phi(3)-1=0.99, where \Phi is the cdf of N(0,1). Indeed:

```julia
julia> using Distributions
julia> d = Normal(0,1);
julia> prob=2*cdf(d, 3)-1
0.9973002039367398

```

Then the graphs look as follows:

```julia
using PlotlyJS, Colors, ColorSchemes

f(x, σ; μ=0)= exp(-0.5*(x-μ)^2/σ^2)/(σ*sqrt(2π))
fig=Plot();
sigmas=0.5:0.25:4.0
n=length(sigmas)
mycolors = ColorScheme([get(ColorSchemes.turbo, t) for t in range(0, 1.0, length=n)])

for (k,σ) ∈ enumerate(sigmas)
    x= range(-3*σ, 3*σ, length=floor(Int,100*σ)) 
    addtraces!(fig, scatter3d(x=x, y=σ*ones(size(x)), z=f.(x, σ), 
                              mode="lines", name="σ: $σ",
                              line_color="#"*hex(mycolors[k]), line_width=4))
end 
relayout!(fig, width=500, height=500, #template=templates["plotly_dark"],
          scene_xaxis_range=[-3*sigmas[end], 3*sigmas[end]],
          scene_camera_eye=attr(x=1.5, y=1.5, z=0.7), font_size=11, 
          )
display(fig)

```

![Npdfs-lines](https://global.discourse-cdn.com/julialang/original/3X/b/4/b491d39b6cbb2fd92d57e737fc2e9350d5e18233.png)

or as surfaces:  
 ![Npdfs-surfs](https://global.discourse-cdn.com/julialang/original/3X/9/e/9e3aab44ae502f9ea7fe56aedac15778cff888b0.png)
