Open8

Juliaでプロットいろいろメモ

yuuyuu

複数プロットする方法

xs = range(0, 20, length = 21)
μs = [0.2, 0.4, 0.6, 0.8]

plt = []

for i in 1:4
    μ = μs[i]
    d = Binomial(20, μ)
    push!(plt, bar(xs, pdf.(d, xs), label = "μ=$(μ)"))
end

plot(plt..., layout = (2, 2), size = (400, 400))

yuuyuu
xs = 0:60

rs = [3, 5, 10]
μs = [0.3, 0.5, 0.7]

plt = []

for i in 1:length(rs)
    for j in 1:length(μs)
        d = NegativeBinomial(rs[i], μs[j])
        push!(plt, bar(xs, pdf.(d, xs), label = "μ=$(μs[i]), r=$(rs[i])"))
    end
end

plot(plt...)

yuuyuu
μ = [0.0, 0.0]
Σ = [0.01 0.0;
     0.0 0.01]
X = [-10, -5, 0, 5, 10]

num_samples = 3

W = rand(MvNormal(μ, Σ), num_samples)

Ys = []

xs = range(-12, 12, length = 100)

plt1 = plot()
plt2 = plot()
for n in 1:num_samples
    w1, w2 = W[:, n]
    scatter!(plt1, (w1, w2))

    f(x) = sig(w1 * x + w2)

    plot!(plt2, xs, f.(xs))

    Y = rand.(Bernoulli.(f.(X)))

    push!(Ys, Y)

end

plot(plt1, plt2, size = (800, 400))

yuuyuu

bar()関数

d = Bernoulli(0.3)
bar([0,1], pdf.(d, [0, 1]))

yuuyuu

histogram()関数

d = Bernoulli(0.3)
x = rand(d, 10000)
b_range = range(0, 20, length=20)
histogram(x, bins=b_range)

yuuyuu

contour()関数

μ = [0.0, 0.0]
Σ = [1.0 0.5;
     0.5 1.0]
d = MvNormal(μ, Σ)
x = rand(d, 100)

contour(
    xs1, xs2,
    [pdf(d, [x1, x2]) for x1 in xs1, x2 in xs2],
    # contour_labels=true
    clabels = true,
    levels = 0.025:0.025:0.150
    )