Skip to content

Commit

Permalink
Merge b992189 into 974d3f5
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer committed Dec 24, 2021
2 parents 974d3f5 + b992189 commit e5ae117
Show file tree
Hide file tree
Showing 4 changed files with 69 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
66 changes: 66 additions & 0 deletions docs/src/makie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Makie.jl plots

Compared to Gadfly.jl and StatsPlots.jl, Makie.jl is the most flexible plotting library that you can use.
On this page is an example function for plotting with Makie.jl which you can use directly or further tweak to your needs.

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

```@example makie
function plot_chains(chns; density_func=density!)
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"
hideydecorations!.(values_axs; label=false)
hidexdecorations!.(values_axs[1:end-1]; grid=false)
# Create and store separate axes for showing the parameter density estimate.
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_func(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)
return fig
end
nothing # hide
```

```@example makie
plot_chains(chns)
```

0 comments on commit e5ae117

Please sign in to comment.