`rowsize!` and `colsize!` for nested layouts

I am writing code which generates a specific layout with interactions etc. It works fine for a figure, but as soon as I try to embed it as a subfigure I am getting into trouble. The reason is that rowsize! and colsize! fail when called for a layout rather than a whole figure. Here is an example:

fig = Figure()
subfigure = fig[1,1]
plot(subfigure[1,1],cos.(1:0.1:10), color=:green)
plot(subfigure[2,1],sin.(1:0.1:10), color=:red)
rowsize!(subfigure.layout,2,Auto(2)) # fails
plot(fig[2,1],sqrt.(1:0.1:10), color=:red)
rowsize!(subfigure.layout,2,Auto(2)) # does not what it is supposed to do, i.e. changing the relative size of the rows in the subfigure

Any idea how to achieve the relative row or column size specification such that is also works for type GridPosition, i.e. a sub-layout?

subfigure is only a GridPosition, the description of a position in a GridLayout but not a GridLayout itself. Do subfigure = fig[1, 1] = GridLayout() to make the layout explicit instead of implicit. Then you can do rowsize!(subfigure,... directly.

1 Like

Thanks for the quick reply. So now this code works:

using GLMakie
fig = Figure()
subfigure = fig[1,1] = GridLayout()
plot(subfigure[1,1],cos.(1:0.1:10), color=:green)
plot(subfigure[2,1],sin.(1:0.1:10), color=:red)
rowsize!(subfigure,2,Auto(2)) # works
plot(fig[2,1],sqrt.(1:0.1:10), color=:red)

but I am still stuck with the problem of writing a function which can receive a region in a figure (i.e. a subfigure) as an input and can put its layout inside this region.
I tried subfigure.layout = GridLayout() but this is not allowed. Is there another way to assign the gridlayout to a subfigure region? Only then can I make a function that behaves like “image” or “plot”.
How can this code example be corrected so that it does what it is supposed to do?

using GLMakie
function show!(fig_or_sub, rel_heigth)    
    fig_or_sub = GridLayout() # how to do this correctly?
    image(fig_or_sub[1,1], rand(10,10))
    plot(fig_or_sub[2,1],sin.(1:0.1:10), color=:red)
    rowsize!(fig_or_sub,2,Auto(rel_heigth)) 
end
fig = Figure()  # example for two sub-figures
show!(fig[1,1], 1)
show!(fig[1,2], 2)

fig = Figure()
show!(fig, 2) # this should also work

Any help is appreciated.

If fig_or_sub is a Figure, you could place the GridLayout at position [1, 1], otherwise you place it at fig_or_sub[] which just means at that position.

function show!(fig_or_sub, rel_heigth)    
    gl = if fig_or_sub isa Figure
        fig_or_sub[1, 1] = GridLayout()
    else
        fig_or_sub[] = GridLayout()
    end
    image(gl[1,1], rand(10,10))
    plot(gl[2,1],sin.(1:0.1:10), color=:red)
    rowsize!(gl,2,Auto(rel_heigth)) 
end

##

fig = Figure()
show!(fig[1,1], 1)
show!(fig[1,2], 2)
fig


##

fig = Figure()
show!(fig, 2)
fig
1 Like

You are a star! Great. The assignment via the empty brackets fig_or_sub[] = GridLayout() solves the problem!