# Plot the distribution curve

**URL:** <https://discourse.julialang.org/t/plot-the-distribution-curve/66133>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 10, 2021, 1:36pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133 "2021-08-10T13:36:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 10, 2021, 1:36pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/1 "2021-08-10T13:36:02Z")

</div>

How to draw a distribution curve？ such as a normal distribution,  
its format is:

Distributions.Normal{Float64}(μ=-0.42117172154916505, σ=1.2253594150160463)

thanks！

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 10, 2021, 1:43pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/2 "2021-08-10T13:43:33Z")

</div>

I assume you mean to plot the curve of PDF

```julia
using Plots, Distributions

julia> L = Normal(1, 1)
Normal{Float64}(μ=1.0, σ=1.0)

julia> plot(x->pdf(L, x))

```

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 10, 2021, 1:49pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/3 "2021-08-10T13:49:33Z")

</div>

I actually have a lot of distributions, do you know how to plot them as below?

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

Thank you very much!

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 10, 2021, 1:51pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/4 "2021-08-10T13:51:14Z")

</div>

10-element Vector{Normal{Float64}}:  
Normal{Float64}(μ=-0.42117172154916505, σ=1.2253594150160463)  
Normal{Float64}(μ=1.4244177594878171, σ=1.2253594150160463)  
Normal{Float64}(μ=-0.7646882557011875, σ=1.2253594150160463)  
Normal{Float64}(μ=-1.4994608515495296, σ=1.2253594150160463)  
Normal{Float64}(μ=0.1588834413196244, σ=1.2253594150160463)  
Normal{Float64}(μ=0.6906462431515679, σ=1.2253594150160463)  
Normal{Float64}(μ=-0.8610106914769075, σ=1.2253594150160463)  
Normal{Float64}(μ=0.39571181451559956, σ=1.2253594150160463)  
Normal{Float64}(μ=-0.7506648963208741, σ=1.2253594150160463)  
Normal{Float64}(μ=1.0347673088571538, σ=1.2253594150160463)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 10, 2021, 1:52pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/5 "2021-08-10T13:52:01Z")

</div>

why would this change anything? You just plot one line for each of them. Essentially with `pdf`, you can get `xs` and `ys` for each of them.

If you want ridge line plot, see:  
[https://kristofferc.github.io/PGFPlotsX.jl/stable/examples/gallery.html#D-Waterfall](https://kristofferc.github.io/PGFPlotsX.jl/stable/examples/gallery.html#D-Waterfall)

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 10, 2021, 2:01pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/6 "2021-08-10T14:01:23Z")

</div>

Ok, thank you very much!!

---

<div class="post-metadata">

**Author:** ![abcde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abcde/32/16180_2.png) [@abcde](https://discourse.julialang.org/u/abcde)\
**Post date:** [August 10, 2021, 2:33pm UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/9 "2021-08-10T14:33:49Z")

</div>

```julia
using Random
using Distributions

Random.seed!(42)
#Generate Data
x_min = -10 # xrange to plot
x_max = 10
μ_min = -5
μ_max = 5

dist = (μ, σ) -> Normal(μ, σ)
# make the set of distributions we're going to plot:
dists = [dist(-6+i, 1+0.3*i) for i in 1:10]
# creates random scatter points:
rnd = rand.(Truncated.(dists, x_min, x_max), 20)
# get the pdf of the dists:
dat_pdf = [(x) -> pdf.(d, x) for d in dists]

# point density for pdfs
x_pnts = collect(x_min:0.05:x_max)

# add redundant points at the ends, for nicer fill:
x_pnts_ext = [[x_pnts[1]]; x_pnts; [x_pnts[end]]]

# define the Axis to which we will push! the contents of the plot
axis = @pgf Axis(
    {
        width = raw"1\textwidth",
        height = raw"0.6\textwidth",
        grid = "both",
        xmax = x_max,
        xmin = x_min,
        zmin = 0,
        "axis background/.style" = { fill = "gray!10" }, # add some beauty
        # this is needed to make the scatter points appear behind the graphs:
        set_layers,
        view = "{49}{25}", # viewpoint
        ytick = collect(0:9),
        ztick = collect(0:0.1:1)
    },
)

# draw a yellow area at the bottom of the plot, centered at μ and 2σ wide.
@pgf area = Plot3(
    {
        no_marks,
        style ="{dashed}",
        color = "black",
        fill = "yellow!60",
        fill_opacity = 0.65,
        # so we can see the grid lines through the colored area:
        on_layer = "axis background"
    },
    Table(x = [dists[1].μ - dists[1].σ, dists[end].μ - dists[end].σ,
               dists[end].μ + dists[end].σ, dists[1].μ + dists[1].σ],
          y = [length(rnd) - 1, 0, 0, length(rnd) - 1],
          z = [0, 0, 0, 0]
         ),
    raw"\closedcycle"
)
push!(axis, area)

# add the slices as individual plots to the common axis
@pgf for i in eachindex(dists)
    scatter = Plot3(
        {
            only_marks,
            color = "red!80",
            mark_options = {scale=0.4},
            # set the markers on the same layer as the plot:
            mark_layer = "like plot",
            on_layer = "axis background"
        },
        Table(x = rnd[i],
              y = (length(dists) - i) * ones(length(rnd[i])),
              z = zeros(length(rnd[i])))
    )
    push!(axis, scatter)

    # add a pdf-curve on top of each second data set
    if i%2 == 1
        curve = Plot3(
            {
                no_marks,
                style = {thick},
                color = "blue"
            },
            Table(x = x_pnts,
                  y = (length(dists) - i) * ones(length(x_pnts)),
                  z = dat_pdf[i](x_pnts))
        )

        # The fill is drawn seperately to handle the the end of the curves nicely.
        # This is an alternative to "\fillbetween"
        fill = Plot3(
            {
                draw = "none",
                fill = "blue",
                fill_opacity = 0.25
            },
            Table(x = x_pnts_ext,
                  y = (length(dists) - i) * ones(length(x_pnts_ext)),
                  z = [[0]; dat_pdf[i](x_pnts); [0]])
        )
        push!(axis, curve, fill)
    end
end

```

I am running this example in pgfplotsx.jl with an error：  
ERROR: LoadError: MethodError: no method matching Table(; x=[-6.3, 0.0, 8.0, -3.7], y=[9, 0, 0, 9], z=[0, 0, 0, 0])  
Closest candidates are:  
Table(::Type…) at C:\Users\dell.julia\packages\ScientificTypesBase\QLxNe\src\ScientificTypesBase.jl:144 got unsupported keyword arguments “x”, “y”, “z”  
Stacktrace:  
[1] top-level scope  
@ untitled-f6199b5ecb0eccd481cb1cf366825034:44  
[2] eval  
@ .\boot.jl:360 [inlined]  
[3] include\_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1116  
in expression starting at untitled-f6199b5ecb0eccd481cb1cf366825034:44  
LoadError: MethodError: no method matching Table(; x=[-6.3, 0.0, 8.0, -3.7], y=[9, 0, 0, 9], z=[0, 0, 0, 0])  
Closest candidates are:  
Table(::Type…) at C:\Users\dell.julia\packages\ScientificTypesBase\QLxNe\src\ScientificTypesBase.jl:144 got unsupported keyword arguments “x”, “y”, “z”  
Stacktrace:  
[1] top-level scope  
@ untitled-f6199b5ecb0eccd481cb1cf366825034:44  
[2] eval  
@ .\boot.jl:360 [inlined]  
[3] include\_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1116  
in expression starting at untitled-f6199b5ecb0eccd481cb1cf366825034:44

---

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 11, 2021, 6:16am UTC](https://discourse.julialang.org/t/plot-the-distribution-curve/66133/10 "2021-08-11T06:16:57Z")

</div>

Plots.jl examples

First example:

```julia
using Distributions
using Plots

Distributions.Normal{T}(; μ, σ) where T = Normal{T}(μ, σ)
normals = [
    Normal{Float64}(μ=-0.42117172154916505, σ=1.2253594150160463)
    Normal{Float64}(μ=1.4244177594878171, σ=1.2253594150160463)
    Normal{Float64}(μ=-0.7646882557011875, σ=1.2253594150160463)
    Normal{Float64}(μ=-1.4994608515495296, σ=1.2253594150160463)
    Normal{Float64}(μ=0.1588834413196244, σ=1.2253594150160463)
    Normal{Float64}(μ=0.6906462431515679, σ=1.2253594150160463)
    Normal{Float64}(μ=-0.8610106914769075, σ=1.2253594150160463)
    Normal{Float64}(μ=0.39571181451559956, σ=1.2253594150160463)
    Normal{Float64}(μ=-0.7506648963208741, σ=1.2253594150160463)
    Normal{Float64}(μ=1.0347673088571538, σ=1.2253594150160463)
] |> x -> sort(x; by=mean)

ys = range(-10, 10; length=300)
m, n = length(normals), length(ys)
plot(legend=false)
for (x, normal) in zip(range(-1.5, 1.5; length=m), normals)
    plot3d!(fill(x, n), ys, pdf.(normal, ys))
end
plot!()

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/b/7b8f9b8010228142c39b39f8ffcd7d21e74d5798.jpeg)

Second example (linear regression):

```julia
using LinearAlgebra
using Distributions
using Plots

dist_true = MvNormal([50, 50], 25^2*[1 1; 1 1.3])
n = 30
sample = rand(dist_true, n)
x, y = sample[1,:], sample[2,:]
X = x .^ (0:1)'
b̂ = X\y # b\hat TAB -> b̂
ŷ = X*b̂
u = norm(y - ŷ)/√(n - 2)
f(x) = evalpoly(x, b̂)
s(x) = u*√(1 + dot([1, f(x)], X'X\[1, f(x)]))
#disty(x) = Normal(f(x), s(x))
disty(x) = LocationScale(f(x), s(x), TDist(n-2))

plot(legend=false, xtick=-100:50:200, ytick=-100:50:200)
for xs in 0:10:100
    ys = range(-50, 150; length=200)
    plot3d!(fill(xs, length(ys)), ys, pdf.(disty(xs), ys))
end
scatter3d!(x, y, zeros(n); ms=2, msc=:auto, color=:red)
xs = range(-30, 130; length=100)
plot3d!(xs, f.(xs), zero(xs); color=:blue)

```

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