Plotting of PDF of distributions not returning correct plot

Using the following code:

using Distributions

dist = Normal() 

plot(pdf.(dist,collect(-2.0:0.1:2.0)))

The plot returns as

The x-axis values are incorrect, is there a reason is it not centred at 0 as it should be?

Should be noted if I do something such as

plot(x->exp(x),collect(-2:0.1:2)) 

then it will return the correct plot.

You essentially have

plot(array)

In that case the xaxis is the index number into the array. You need something like plot(xarray, yarray), i.e.

x = collect(-2.0:0.1:2.0)
plot(x, pdf.(dist, x))

Alternatively you can plot a function in a range with Plots.jl via

pdf_eval(x) = pdf(dist, x)
plot(pdf_eval, -2, 2)

#equivalently
plot(x->pdf(dist,x), -2, 2)
2 Likes

Or, using the Distribution recipe implemented StatsPlots:

using StatsPlots, Distributions
plot(Normal())
1 Like