How to Determine GridPosition of an Axis in a Complex Figure Layout?

Hello everyone,

I am writing a function using Makie.jl for some custom plotting.

The function takes a Figure and an Axis object as arguments. Although I can safely assume that the axis is somewhere within the figure, its position could be arbitrarily complex.
For instance, the figure could contain multiple cascading GridLayouts and the axis could be a direct child of one of these nested layouts.

Is there a way to determine which GridPosition the axis has been assigned to?

Here’s a minimal example to illustrate what I’m trying to achieve:

using CairoMakie
  
fig = Figure()
  
ax_left = Axis(fig[1, 1])
ax_right = fig[1, 2] = GridLayout()
  
ax_top = Axis(ax_right[1, 1])
ax_bottom = Axis(ax_right[2, 1])
  
function get_gridpos(fig, ax)
    # ...
end
  
get_gridpos(fig, ax_bottom) === ax_right[2, 1] # I'd like this to be true

Any help or pointers would be greatly appreciated!

I don’t remember if I have a convenience function for this somewhere but this is how to get the parts and assemble it manually:

julia> f = Figure()

julia> gp = f[3:4, 8]
GridPosition(GridLayout[1, 1] (0 children), GridLayoutBase.Span(3:4, 8:8), GridLayoutBase.Inner())

julia> ax = Axis(gp)
Axis with 0 plots:


julia> sp = Makie.GridLayoutBase.gridcontent(ax).span
GridLayoutBase.Span(3:4, 8:8)

julia> si = Makie.GridLayoutBase.gridcontent(ax).side
GridLayoutBase.Inner()

julia> gl = Makie.GridLayoutBase.gridcontent(ax).parent
GridLayout[1:4, 1:8] with 1 children
 ┗━ [3:4, 8] Axis


julia> gp2 = GridPosition(gl, sp, si)
GridPosition(GridLayout[4, 8] (1 children), GridLayoutBase.Span(3:4, 8:8), GridLayoutBase.Inner())

julia> gp == gp2
true
3 Likes

Thanks a lot! The code you shared works like a charm.

I just wanted to add one more detail in case anyone else is trying to do something similar. My goal was to replace the axis with an additional GridLayout and plot some things inside it. However, when I initially tried creating the GridLayout using:

layout = gp2 = GridLayout()

as in the examples in the documentation (e.g., here), I got the following error:

Found nothing as the top parent of this GridPosition. 
A GridPosition or GridSubposition needs to be connected to the top layout of a Figure, Scene or comparable object, either directly or through nested GridLayouts in order to plot into it.

After some trial and error, I got it working by defining the layout like this:

layout = GridLayout(gridPos)

There’s actually a function in GridLayoutBase that kinda does this GridLayoutBase.jl/src/gridlayout.jl at master · jkrumbiegel/GridLayoutBase.jl · GitHub