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

**URL:** <https://discourse.julialang.org/t/how-to-determine-gridposition-of-an-axis-in-a-complex-figure-layout/122448>\
**Category:** Visualization\
**Tags:** makie, cairomakie\
**Created:** [November 9, 2024, 11:02am UTC](https://discourse.julialang.org/t/how-to-determine-gridposition-of-an-axis-in-a-complex-figure-layout/122448 "2024-11-09T11:02:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LorenzoFioroni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorenzofioroni/32/213290_2.png) [@LorenzoFioroni](https://discourse.julialang.org/u/LorenzoFioroni)\
**Post date:** [November 9, 2024, 11:02am UTC](https://discourse.julialang.org/t/how-to-determine-gridposition-of-an-axis-in-a-complex-figure-layout/122448/1 "2024-11-09T11:02:47Z")

</div>

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 `GridLayout`s 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:

```julia
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!

---

<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:** [November 9, 2024, 12:11pm UTC](https://discourse.julialang.org/t/how-to-determine-gridposition-of-an-axis-in-a-complex-figure-layout/122448/2 "2024-11-09T12:11:55Z")

</div>

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
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

```

---

<div class="post-metadata">

**Author:** ![LorenzoFioroni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lorenzofioroni/32/213290_2.png) [@LorenzoFioroni](https://discourse.julialang.org/u/LorenzoFioroni)\
**Post date:** [November 9, 2024, 2:29pm UTC](https://discourse.julialang.org/t/how-to-determine-gridposition-of-an-axis-in-a-complex-figure-layout/122448/3 "2024-11-09T14:29:43Z")

</div>

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:

```julia
layout = gp2 = GridLayout()

```

as in the examples in the documentation (e.g., [here](https://docs.makie.org/stable/tutorials/layout-tutorial#Setting-up-GridLayouts)), I got the following error:

```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:

```julia
layout = GridLayout(gridPos)

```

---

<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:** [November 9, 2024, 2:42pm UTC](https://discourse.julialang.org/t/how-to-determine-gridposition-of-an-axis-in-a-complex-figure-layout/122448/4 "2024-11-09T14:42:42Z")

</div>

There’s actually a function in GridLayoutBase that kinda does this [GridLayoutBase.jl/src/gridlayout.jl at master · jkrumbiegel/GridLayoutBase.jl · GitHub](https://github.com/jkrumbiegel/GridLayoutBase.jl/blob/master/src/gridlayout.jl#L594)
