Skip to content

Commit

Permalink
Merge 75e0996 into 974d3f5
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer committed Dec 25, 2021
2 parents 974d3f5 + 75e0996 commit 4985ef5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand All @@ -9,6 +10,7 @@ MLJDecisionTreeInterface = "c6f25543-311c-4c74-83dc-3ea6d1015661"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"

[compat]
CairoMakie = "0.6"
CategoricalArrays = "0.8, 0.9, 0.10"
DataFrames = "0.22, 1"
Documenter = "0.26, 0.27"
Expand Down
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ makedocs(
"Getting started" => "getting-started.md",
"Plotting" => [
"StatsPlots.jl" => "statsplots.md",
"Makie.jl" => "makie.md",
"Gadfly.jl" => "gadfly.md"
],
"API" => [
Expand Down
9 changes: 0 additions & 9 deletions docs/src/gadfly.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ using Gadfly
write_svg(path, p; w=6inch, h=4inch) = Gadfly.draw(Gadfly.SVG(path, w, h), p) # hide
using MCMCChains
# Define the experiment.
n_iter = 100
n_name = 3
n_chain = 2
# Experiment results.
val = randn(n_iter, n_name, n_chain) .+ [1, 2, 3]'
val = hcat(val, rand(1:2, n_iter, 1, n_chain))
chn = Chains(randn(100, 2, 3), [:A, :B])
df = DataFrame(chn)
df[!, :chain] = categorical(df.chain)
Expand Down
78 changes: 78 additions & 0 deletions docs/src/makie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Makie.jl plots

This page shows an example of plotting MCMCChains.jl with Makie.jl.
The example is meant to provide an useful basis to build upon.
Let's define some random chain and load the required packages:

```@example makie
using MCMCChains
chns = Chains(randn(300, 5, 3), [:A, :B, :C, :D, :E])
```

A basic way to visualize the chains is to show the drawn samples at each iteration.
Colors depict different chains:

```@example makie
using CairoMakie
CairoMakie.activate!(type = "svg")
params = names(chns, :parameters)
n_chains = length(chains(chns))
n_samples = length(chns)
fig = Figure(; resolution=(1_000, 800))
function chain_values(chns, chain::Int, param::Symbol)
return get(chns, param)[param][:, chain]
end
# Create and store separate axes for showing iterations.
values_axs = [Axis(fig[i, 1]; ylabel=string(c)) for (i, c) in enumerate(params)]
for (ax, param) in zip(values_axs, params)
for chain in 1:n_chains
values = chain_values(chns, chain, param)
lines!(ax, 1:n_samples, values; label=string(chain))
end
end
# Thanks to having stored the axes before, we can apply some extra tweaks.
# These tweaks usually depend on the kind of model being fitted.
values_axs[end].xlabel = "Iteration"
hidexdecorations!.(values_axs[1:end-1]; grid=false)
fig
```

Next, we can add a second row of plots next to it which show the density estimate for these samples:

```@example makie
density_axs = [Axis(fig[i, 2]; ylabel=string(c)) for (i, c) in enumerate(params)]
for (ax, param) in zip(density_axs, params)
for chain in 1:n_chains
values = chain_values(chns, chain, param)
density!(ax, values; label=string(chain))
end
end
# Just like above, we add some extra tweaks.
density_axs[end].xlabel = "Parameter estimate"
linkxaxes!(density_axs...)
hideydecorations!.(density_axs)
hidexdecorations!.(density_axs[1:end-1]; grid=false)
# And we can also drop the y ticks for the plots on the left.
hideydecorations!.(values_axs; label=false)
fig
```

Finally, let's add a simple legend.
Thanks to setting `label` above, this legend will have the right labels:

```@example makie
axislegend(first(density_axs))
fig
```
2 changes: 1 addition & 1 deletion src/chains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Base.lastindex(c::Chains, d::Integer) = lastindex(c.value, d)
Base.get(c::Chains, v::Symbol; flatten=false)
Base.get(c::Chains, vs::Vector{Symbol}; flatten=false)
Return a `NamedTuple` with `v` as the key, and matching paramter
Return a `NamedTuple` with `v` as the key, and matching parameter
names as the values.
Passing `flatten=true` will return a `NamedTuple` with keys ungrouped.
Expand Down

0 comments on commit 4985ef5

Please sign in to comment.