Skip to content

Commit

Permalink
nicer plot
Browse files Browse the repository at this point in the history
  • Loading branch information
alecloudenback committed Mar 29, 2024
1 parent 39ee2cf commit 8b1fb82
Showing 1 changed file with 29 additions and 59 deletions.
88 changes: 29 additions & 59 deletions ext/MCMCChainsMakieExt.jl
Expand Up @@ -4,75 +4,35 @@ import MCMCChains
import Makie


# @recipe(MCMCChains.Chains) do scene
# Theme()
# end

function Makie.plot!(chns::MCMCChains.Chains)
params = names(chns, :parameters)

n_chains = length(chains(chns))
n_samples = length(chns)

fig = Figure()

for (i, param) in enumerate(params)
ax = Axis(fig[i, 1]; ylabel=string(param))
for chain in 1:n_chains
values = chns[:, param, chain]
lines!(ax, 1:n_samples, values; label=string(chain))
end

hideydecorations!(ax; label=false)
if i < length(params)
hidexdecorations!(ax; grid=false)
else
ax.xlabel = "Iteration"
end
end

for (i, param) in enumerate(params)
ax = Axis(fig[i, 2]; ylabel=string(param))
for chain in 1:n_chains
values = chns[:, param, chain]
density!(ax, values; label=string(chain))
end

hideydecorations!(ax)
if i < length(params)
hidexdecorations!(ax; grid=false)
else
ax.xlabel = "Parameter estimate"
end
end

axes = [only(contents(fig[i, 2])) for i in 1:length(params)]
linkxaxes!(axes...)

axislegend(first(axes))

return fig
end


function MCMCChains.myplot(chns::T) where {T<:MCMCChains.Chains}
params = names(chns, :parameters)

Check warning on line 8 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L7-L8

Added lines #L7 - L8 were not covered by tests

n_chains = length(MCMCChains.chains(chns))
n_samples = length(chns)
n_params = length(params)

Check warning on line 12 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L10-L12

Added lines #L10 - L12 were not covered by tests

colors = Makie.cgrad(:roma100, n_chains, categorical=true)
fig = Makie.Figure()
colors = Makie.to_colormap(:tol_vibrant)
width = 600
height = max(400, 80 * n_params)

Check warning on line 16 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L14-L16

Added lines #L14 - L16 were not covered by tests

fig = Makie.Figure(; size=(width, height))

Check warning on line 18 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L18

Added line #L18 was not covered by tests

for (i, param) in enumerate(params)
ax = Makie.Axis(fig[i+1, 1]; ylabel=string(param))
for chain in 1:n_chains
values = chns[:, param, chain]
Makie.lines!(ax, 1:n_samples, values; label=string(chain), color=(colors[chain], 0.7), linewidth=0.7)
Makie.lines!(

Check warning on line 24 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L20-L24

Added lines #L20 - L24 were not covered by tests
ax,
1:n_samples,
values;
label=string(chain),
color=(colors[chain], 0.7),
linewidth=0.7
)
end

Check warning on line 32 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L32

Added line #L32 was not covered by tests

Makie.hideydecorations!(ax; label=false)
if i < length(params)
if i < n_params
Makie.hidexdecorations!(ax; grid=false)

Check warning on line 36 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L34-L36

Added lines #L34 - L36 were not covered by tests
else
ax.xlabel = "Iteration"

Check warning on line 38 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L38

Added line #L38 was not covered by tests
Expand All @@ -83,21 +43,31 @@ function MCMCChains.myplot(chns::T) where {T<:MCMCChains.Chains}
ax = Makie.Axis(fig[i+1, 2]; ylabel=string(param))
for chain in 1:n_chains
values = chns[:, param, chain]
Makie.density!(ax, values; label=string(chain), color=(colors[chain], 0.7))
Makie.density!(

Check warning on line 46 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L42-L46

Added lines #L42 - L46 were not covered by tests
ax,
values;
label=string(chain),
color=(colors[chain], 0.1),
strokewidth=1,
strokecolor=(colors[chain], 0.7)
)
end

Check warning on line 54 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L54

Added line #L54 was not covered by tests

Makie.hideydecorations!(ax)
if i < length(params)
if i < n_params
Makie.hidexdecorations!(ax; grid=false)

Check warning on line 58 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L56-L58

Added lines #L56 - L58 were not covered by tests
else
ax.xlabel = "Parameter estimate"

Check warning on line 60 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L60

Added line #L60 was not covered by tests
end
end

Check warning on line 62 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L62

Added line #L62 was not covered by tests

axes = [only(Makie.contents(fig[i+1, 2])) for i in 1:length(params)]
axes = [only(Makie.contents(fig[i+1, 2])) for i in 1:n_params]
Makie.linkxaxes!(axes...)

Check warning on line 65 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L64-L65

Added lines #L64 - L65 were not covered by tests

Makie.Legend(fig[1, 1:2], first(axes), "Chain", orientation=:horizontal)
Makie.Legend(fig[1, 1:2], first(axes), "Chain", orientation=:horizontal, titlehalign=:left, halign=:left, titleposition=:left)

Check warning on line 67 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L67

Added line #L67 was not covered by tests

Makie.rowgap!(fig.layout, 10)
Makie.colgap!(fig.layout, 10)

Check warning on line 70 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L69-L70

Added lines #L69 - L70 were not covered by tests

return fig

Check warning on line 72 in ext/MCMCChainsMakieExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/MCMCChainsMakieExt.jl#L72

Added line #L72 was not covered by tests
end
Expand Down

0 comments on commit 8b1fb82

Please sign in to comment.