# Plotting MCMCChains with Makie

**URL:** <https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860>\
**Category:** Visualization\
**Tags:** turing, makie\
**Created:** [July 27, 2022, 8:50am UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860 "2022-07-27T08:50:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [July 27, 2022, 8:50am UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/1 "2022-07-27T08:50:47Z")

</div>

It is currently trivially simple to plot the posterior likelihood functions from a MCMC chain with StatsPlots. However, it was quite easy to make a function that allows the same automatic plotting with Makie:

> **Setup**
>
> ```julia
> julia> using Turing
> 
> julia> @model function normalmodel(y)
> # Our prior belief about the probability
> 
> μ ~ Uniform(extrema(y)...)
> σ ~ Uniform(0, 10^3) # σ must be positive
> 
> # The number of observations.
> N = length(y)
> 
> # Each sample is distributed as Normal(μ, σ)
> y .~ filldist(Normal(μ, σ), N)
> end
> 
> julia> N_samples = 20;
> 
> julia> N_MCMC = 10^3;
> 
> julia> μ_true = 15;
> 
> julia> σ_true = 1;
> 
> julia> data_normal = rand(Normal(μ_true, σ_true), N_samples);
> 
> julia> normal_instance = normalmodel(data_normal);
> 
> julia> Threads.nthreads()
> 4
> 
> julia> chain_normal = sample(normal_instance, NUTS(), MCMCThreads(), N_MCMC, 4);
> 
> ```

> **Ploting function definition**
>
> ```julia
> julia> using GLMakie
> 
> julia> import Makie.plot
> 
> julia> function Makie.plot(ch::Chains)
> fig = Figure()
> for (ind, param) in enumerate(ch.name_map.parameters)
> ax = Axis(fig[ind, 1], title=string(param))
> for (ind2, datavec) in enumerate(eachcol(getindex(ch, param).data))
> # Get current default colorpalette
> colors = Makie.current_default_theme().attributes[:palette][][:color][]
> density!(ax, datavec, color=(:black, 0.0),
> strokearound=true,
> strokewidth=2,
> strokecolor=colors[ind2%length(colors)]
> )
> end
> end
> display(fig)
> return fig
> end
> 
> ```

allows the following line

```julia
julia> plot(chain_normal)

```

to produce the following plot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/0/e02962730ad154d027143de3c38b6b341d829e7b.png)

Would it be possible to make this work by default? E.g. defining a recipe for Chains within Makie?

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [July 27, 2022, 3:24pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/2 "2022-07-27T15:24:08Z")

</div>

I would say something like this is better off in a glue package. For base Makie I don’t think the convenience for the people who want this would be worth the inconvenience of extra dependencies, compile and using time for all.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 27, 2022, 4:01pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/3 "2022-07-27T16:01:46Z")

</div>

This stuff actually works pretty well with AlgebraOfGraphics, one just needs to transform the chains into appropriate table objects. They are already tables but not quite in the right format

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [July 27, 2022, 7:28pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/4 "2022-07-27T19:28:11Z")

</div>

It indeed did until a few days ago. Then it started to complain about a colormap if compiled with GLMakie. I wish there was something like `fillcolor=:none` in Makie’s density().

```julia
ERROR: LoadError: KeyError: key :colormap not found
Stacktrace:
  [1] getindex(h::Dict{Symbol, Observables.Observable}, key::Symbol)
    @ Base ./dict.jl:484
  [2] getindex
    @ ~/.julia/packages/MakieCore/8YGMv/src/attributes.jl:96 [inlined]
  [3] getindex(x::MakieCore.Mesh{Tuple{GeometryBasics.Mesh{2, Float32, GeometryBasics.Ngon{2, Float32, 3, GeometryBasics.Point{2, Float32}}, GeometryBasics.SimpleFaceView{2, Float32, 3, GeometryBasics.OffsetInteger{-1, UInt32}, GeometryBasics.Point{2, Float32}, GeometryBasics.NgonFace{3, GeometryBasics.OffsetInteger{-1, UInt32}}}}}}, key::Symbol)
    @ MakieCore ~/.julia/packages/MakieCore/8YGMv/src/attributes.jl:192

```

---

<div class="post-metadata">

**Author:** ![sethaxen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sethaxen/32/35604_2.png) [@sethaxen](https://discourse.julialang.org/u/sethaxen)\
**Post date:** [July 27, 2022, 9:45pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/5 "2022-07-27T21:45:24Z")

</div>

FWIW, there’s more than one way to convert MCMC draws to a table. `MCMCChains.Chains` takes an approach that mirrors how `Chains` itself flattens multidimensional outputs. ArviZ.jl, which can consume `Chains` objects, takes a different approach. Both implement the Tables interface and can be used with StatsPlots.jl and AlgebraOfGraphics.jl, but which approach is best for you may vary from plot to plot.

Here are some examples of using ArviZ.jl with AlgebraOfGraphics: [Creating custom plots · ArviZ.jl](https://julia.arviz.org/stable/creating_custom_plots/)

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [July 28, 2022, 8:07pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/6 "2022-07-28T20:07:57Z")

</div>

Could someone provide example code of how to transform the samples into a table in a format consumable by AlgebraOfGraphics? Ideally a MWE like the one I provided, but using AlgebraOfGraphics

---

<div class="post-metadata">

**Author:** ![sethaxen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sethaxen/32/35604_2.png) [@sethaxen](https://discourse.julialang.org/u/sethaxen)\
**Post date:** [July 28, 2022, 9:24pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/7 "2022-07-28T21:24:43Z")

</div>

This about does it.

```julia
using AlgebraOfGraphics, GLMakie
using AlgebraOfGraphics: density
fig = Figure()
for (i, var_name) in enumerate((:μ, :σ))
    draw!(
        fig[i, 1],
        data(chain_normal) *
        mapping(var_name; color=:chain => nonnumeric) *
        density() *
        visual(fillalpha=0)
    )
end

```

 ![tmp](https://global.discourse-cdn.com/julialang/original/3X/6/8/685a90cb6361c28f8957beee54028ccc28742efc.png)

The main difference from the plot in the OP is that AlgebraOfGraphics uses the variable name as the x-axis label instead of the title, which is sensible since that’s what the ticks correspond to.

We need to loop over the variable names in this case because they’re found in different columns.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 29, 2022, 1:13pm UTC](https://discourse.julialang.org/t/plotting-mcmcchains-with-makie/84860/8 "2022-07-29T13:13:06Z")

</div>

You can also stack the DataFrame into long format, then you don’t have to loop. I always forget how to use AoG with wide format effectively
