Makie workflow to access and change attributes of objects

Disclaimer: This relies on Makie, GridLayoutBase internals and does not work for any plot elements (like lines, scatter, ...). Tested on Makie v0.18.0. (I am sorry @jules, but I could not resist :slight_smile: )

using Makie
using GLMakie
GLMakie.activate!()

function delete!(f::Figure, blk::Makie.Block)
    # remove from scene
    idx = findfirst(s -> s === blk.blockscene, f.scene.children)
    isnothing(idx) && error("could not find block in figure's scenes")
    deleteat!(f.scene.children, idx)
    layout = blk.layoutobservables.gridcontent[]
    # remove from layout
    l_idx = findfirst(l -> l === layout, f.layout.content)
    isnothing(l_idx) && error("could not find block in figure's layout")
    prev_parent = layout.parent
    Makie.GridLayoutBase.remove_from_gridlayout!(f.layout.content[l_idx])
    layout.parent = prev_parent
    Makie.GridLayoutBase.trim!(f.layout)
    return f # trigger a redraw to remove the blockscene's drawings
end

f = Figure()
ax = Axis(f[1,1])
lines!(ax, 1:3, 1:3, label="line1")
leg = Legend(f[1,2], ax)

display(f) # layout =^= [ ax, leg ]
delete!(f, leg) # layout =^= [ ax ]

EDIT: I think the above does not work properly if you try to remove a block that is not located at the boundary of a layout. E.g. if you have a layout like (only 1 row) [ ax1, ax2, ax3 ] and you call delete!(f, ax2) then the gap between ax1 and ax3 will not be removed… Nevermind, it seems to work.