Makie - Colorbar in plot recipe

TLDR: Is it possible to add a Colorbar directly inside a plot recipe ?

I have a graph-like data structure called MTG where each node stores a mesh and attributes. I made a recipe that extends the MeshViz.jl Viz recipe to my input type (by the way is this type piracy or is it fine to do that?). What the recipe does is recursively call viz! (from MeshViz.jl) to plot each mesh into the scene. Now the user can color each mesh based on an attribute, such as the area of the mesh or whatever.

To do so, the recipe computes the range of values found in the MTG and then get the color of each mesh based on that range, and pass that color to each call to viz!() (one for each mesh). Now if the user needs a Colorbar, it will work, and here’s an example for a coffee plant:

# ]add https://github.com/VEZY/PlantGeom.jl
using PlantGeom
using CairoMakie

file = joinpath(dirname(dirname(pathof(PlantGeom))), "test", "files", "coffee.opf")
opf = read_opf(file)
f, ax, p = viz(opf, color = :Area)
Colorbar(f, label = "Area", colormap = :viridis)

But of course the range of values is wrong (0:1), they should span from ~0.1-132.0.

I would like to build the Colorbar directly inside the recipe to avoid this, but then I need access to the figure apparently, and i don’t know how to do that.

I found a somewhat similar question here but the solution doesn’t work with the latest version of Makie. I tried replacing Makie.FigurePosition with Makie.GridPosition with no success.

The recipe in question is here: https://github.com/VEZY/PlantGeom.jl/blob/main/src/makie_recipes/opf_recipe.jl
It extends the following recipe: https://github.com/JuliaGeometry/MeshViz.jl/blob/f042791a6d44caa302ece57678c4c5b4e9635899/src/MeshViz.jl#L47

For the record, what I would like to get can be achieved like this:

# ]add https://github.com/VEZY/PlantGeom.jl
using PlantGeom
using CairoMakie

file = joinpath(dirname(dirname(pathof(PlantGeom))), "test", "files", "coffee.opf")
opf = read_opf(file)
range_val = extrema(descendants(opf, :Area, ignore_nothing = true))
f = Figure()
viz(f[1, 1], opf, color = :Area)
Colorbar(f[1, 2], label = "Area", colormap = :viridis, limits = Float64.(range_val))
f

But I don’t want to put the burden of doing all that on the user, that’s why I would like to compute it inside the recipe if possible. Any idea how I could do that ?

@rvezy I am adding a new function to MeshViz.jl to perform more steps besides displaying the mesh with colors. It will include the colorbar and other interactive elements. Hopefully will get it done by next week.

I don’t think Makie allows specifying colorbars inside recipes yet. We need to write normal Julia functions that call the recipes we already have and generate the scene we want.

BTW, nice visualization :100:

1 Like