# What is the difference btween handling f\[i,j\]=GridLayout() and g=f\[i,j\]=GridLayout()

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675>\
**Category:** Visualization\
**Tags:** makielayout\
**Created:** [September 4, 2021, 3:29am UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675 "2021-09-04T03:29:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![thudjx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thudjx/32/15231_2.png) [@thudjx](https://discourse.julialang.org/u/thudjx)\
**Post date:** [September 4, 2021, 3:29am UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/1 "2021-09-04T03:29:47Z")

</div>

I am trying to plot a 2x2 figure as shown below.

 ![Makie_example](https://global.discourse-cdn.com/julialang/original/3X/7/2/7234abcbd3badaf00e36bfcc978aa73e4bcc3c6b.png)  
So I

```julia
import CairoMakie
for ir in 1:2, ic in 1:2
    g = f[ir,ic] = GridLayout()
    # handling g below
end

```

But if I try the following code:

```julia
 for ir in 1:2, ic in 1:2
     f[ir,ic] = GridLayout()
     # handling f[ir,ic] directly below
end

```

By handing I mean codes like:

```julia
Axis(g[1,1]);
colsize!(g, 1, Relative(2/3))
colgap!(g, 0)
Label(g[1, 1:2, Top()], gridtitles[ir][ic], textsize = 26)

```

, or

```julia
Axis(f[ir,ic][1,1]);
colsize!(f[ir,ic], 1, Relative(2/3))
colgap!(f[ir,ic], 0)
Label(f[ir,ic][1, 1:2, Top()], gridtitles[ir][ic], textsize = 26)

```

My question is, when handled by `g` everything works fine; but not by `f[ir,ic]`. Here is the error info:

```julia
ERROR: LoadError: MethodError: no method matching colsize!(::GridPosition, ::Int64, ::Relative)
Closest candidates are:
  colsize!(::GridLayout, ::Int64, ::GridLayoutBase.ContentSize) at /home/dongjx/.julia/packages/GridLayoutBase/nYdeK/src/gridlayout.jl:552
  colsize!(::GridLayout, ::Int64, ::Real) at /home/dongjx/.julia/packages/GridLayoutBase/nYdeK/src/gridlayout.jl:560

```

This is quite weird to me, because in my view `g = f[ir,ic] = GridLayout()` is nothing but renaming `f[ir,ic]` as `g`, and I should handle them the same.  
Why is that?

---

<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:** [September 4, 2021, 5:51am UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/2 "2021-09-04T05:51:58Z")

</div>

It’s explained here

[http://makie.juliaplots.org/stable/documentation/figure/#retrieving\_objects\_from\_a\_figure](http://makie.juliaplots.org/stable/documentation/figure/#retrieving_objects_from_a_figure)

---

<div class="post-metadata">

**Author:** ![thudjx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thudjx/32/15231_2.png) [@thudjx](https://discourse.julialang.org/u/thudjx)\
**Post date:** [September 4, 2021, 10:16am UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/3 "2021-09-04T10:16:34Z")

</div>

Thanks for your reply! I have read the documentation and some related material, but still kind of confused about `GridLayout` and `GridPosition`. The following are my naive understanding of them.

The `GridLayout` is the container of `GridLayout` and layoutables, just as the role played by directories, to contain directories and files. In this way, we build a tree structure to contain a bunch of elements.  
The `GridPostion` is a label on `GridLayout` indicating its postion in a figure.

I draw the testing figure:

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

And the structure of `GridLayout` of `f` is:

```julia
infil> f.layout
GridLayout[2, 2] with 3 children
 ┣━ [1, 1] Axis
 ┣━ [1, 2] GridLayout[2, 2] with 3 children
 ┃ ┣━ [1, 1] Colorbar
 ┃ ┣━ [2, 1] Axis
 ┃ ┗━ [1:2, 2] Axis
 ┃
 ┗━ [2, 1:2] Axis

```

The brakets leading each line are the corresponding `GridPosition`s.  
When we using functions like `colsize!` and `colgap`, we need to indicate which `GridLayout` we are manipulating.  
My questions are:

1. why we bother introduce `GridSubposition`? As in my shollow understanding, `GridPosition` can do all the job.
2. how can we refer to the `GridLayout` living in `f[1,2]`. I have tried something like below but failed(my initial question of this topic). Must I do something like `g = f[1,2] = GridLayout()` and refer to it by `g`? Because in my testing example `GridLayout` is built implicitly, I think this question is quite natural.

```julia
infil> f[1,2].layout
GridLayout[2, 2] with 3 children
┣━ [1, 1] Axis
┣━ [1, 2] GridLayout[2, 2] with 3 children
┃ ┣━ [1, 1] Colorbar
┃ ┣━ [2, 1] Axis
┃ ┗━ [1:2, 2] Axis
┃
┗━ [2, 1:2] Axis

```

3. As the codes in 2 shows, the returning of `f[1,2].layout` is exatly `f.layout`. Does this make any sense?

---

<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:** [September 4, 2021, 11:18am UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/4 "2021-09-04T11:18:33Z")

</div>

A GridPosition stores a position in a specific GridLayout. `gp.layout` returns that layout. A GridSubposition refers to a position in a theoretical GridLayout that could exist in a GridSubposition or GridPosition. When an object is instantiated at a GridSubposition, possibly missing parent layouts are created implicitly. You can get a layout at a certain GridPosition with `content(gp)`, this can retrieve any layoutable object that can be in a certain GridPosition.

There’s also the internal GridLayoutBase function `get_layout_at!` which handles the implicit GridLayout creation.

---

<div class="post-metadata">

**Author:** ![thudjx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thudjx/32/15231_2.png) [@thudjx](https://discourse.julialang.org/u/thudjx)\
**Post date:** [September 4, 2021, 12:45pm UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/5 "2021-09-04T12:45:39Z")

</div>

Ok, let me ask a simple a question.

`Figure` and `GridPosition` both has a field name `:layout`, so I guess they should contain the same kind of infomation.

```julia
julia> fieldnames(Figure)
(:scene, :layout, :content, :attributes, :current_axis)

julia> fieldnames(GridPosition)
(:layout, :span, :side)

```

Let’s make a simple figure:

```julia
f = Figure()
Axis(f[1,1])
Axis(f[1,2][1,1])

```

The layout of `f` makes perfect sense:

```julia
julia> f.layout
GridLayout[1, 2] with 2 children
 ┣━ [1, 1] Axis
 ┗━ [1, 2] GridLayout[1, 1] with 1 children
   ┗━ [1, 1] Axis

```

, while layout of `f[1,2]` doesn’t:

```julia
julia> f[1,2].layout
GridLayout[1, 2] with 2 children
 ┣━ [1, 1] Axis
 ┗━ [1, 2] GridLayout[1, 1] with 1 children
   ┗━ [1, 1] Axis

```

As shown in `f.layout`, `f[1,2]` is the `GridPostion` for the `GridLayout[1, 1] with 1 children`. But we cannot work with `f[1,2]` except when using it to build layoutables. Anyway, `f.layout` and `f[1,2].layout` return exactly the samething, which doesn’t make sense to me.

---

<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:** [September 4, 2021, 12:54pm UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/6 "2021-09-04T12:54:40Z")

</div>

As I said, the layout field of the GridPosition refers to the parent layout, which is the same as the top layout of the figure. If there is a GridLayout at that position (that’s not a given that there is one, generally speaking) you can access it via `content(gridposition)` which looks up the object stored at that location.

---

<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:** [September 4, 2021, 12:57pm UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/7 "2021-09-04T12:57:01Z")

</div>

Also, the struct field names (like usual in Julia) are not really part of the public api, they just happen to be useful sometimes so you read about them here and there.

---

<div class="post-metadata">

**Author:** ![thudjx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thudjx/32/15231_2.png) [@thudjx](https://discourse.julialang.org/u/thudjx)\
**Post date:** [September 4, 2021, 1:37pm UTC](https://discourse.julialang.org/t/what-is-the-difference-btween-handling-f-i-j-gridlayout-and-g-f-i-j-gridlayout/67675/8 "2021-09-04T13:37:46Z")

</div>

But I cannot pass it to functions like `colsize!`, `colgap!` which takes a `GridLayout`. So I have to use this `g = f[1,2] = GridLayout()` statement, which is kind of tedious to me. I undertant that to make things easy `f[1,2]` always return a `GridPosition`; to get the contents in a gridposition we can use `content(gp)/contents(gp)`; but why dont we just return the `GridLayout` in place by `f[1,2].layout` so that we can pass it directly as the layout to spare another annotation?
