Skip to content

Commit

Permalink
Merge 5805aea into 974d3f5
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer committed Dec 24, 2021
2 parents 974d3f5 + 5805aea commit 0f515dc
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 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
79 changes: 79 additions & 0 deletions docs/src/makie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# 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 CairoMakie
using DataFrames
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
params = names(chns, :parameters)
df = DataFrame(chns)
n_chains = length(unique(df.chain))
n_samples = nrow(df) / n_chains
# Alternatively, use `CategoricalArrays.categorical`.
df[!, :chain] = string.(df.chain)
fig = Figure(; resolution=(1_000, 800))
# 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, col) in zip(values_axs, params)
for i in 1:n_chains
chain = string(i)
values = filter(:chain => ==(chain), df)[:, col]
lines!(ax, 1:n_samples, values; label=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, col) in zip(density_axs, params)
for i in 1:n_chains
chain = string(i)
values = filter(:chain => ==(chain), df)[:, col]
density!(ax, values; label=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 entries:

```@example makie
axislegend(first(density_axs))
fig
```

0 comments on commit 0f515dc

Please sign in to comment.