Code
using Distributions,GLMakie,Random
Random.seed!(343434)TaskLocalRNG()
通过contour:levels参数控制曲线的范围
levels=1 则只绘制出一条等位线
using Distributions,GLMakie,Random
Random.seed!(343434)TaskLocalRNG()
μ=[3,2]
Σ=[1 -1.5;-1.5 4]
d=MvNormal(μ,Σ)
xs=range(0,6,100)
ys=range(-4,7,100)
dens_mvnormal = [pdf(d, [i, j]) for i in xs, j in ys]
rand_sample=rand(d,100)2×100 Matrix{Float64}:
2.04893 3.14939 2.04006 2.96766 3.14777 … 2.9465 2.47969 3.17612
3.31172 2.04522 2.43717 1.83891 1.69139 2.44297 0.862216 0.327061
function plot_func(fig,idx,levels=1)
ax=Axis(fig[1,idx], xlabel=L"X", ylabel=L"Y")
Box(fig[1,idx];color = (:orange,0.1))
scatter!(ax,rand_sample;marker=:circle,markersize=10,color=(:green,0.1),
strokewidth=1,strokecolor=:black)
contour!(ax,xs, ys,dens_mvnormal;levels=levels,linewidth=1)
end
function plot_mvnormal()
fig=Figure(resolution=(800,400))
[plot_func(fig,idx,levels) for (idx,levels) in enumerate([1,16])]
fig
end
plot_mvnormal()