What is a good way to delete lines in Makie?

I have a diagram in a layout that is changing depending on user input. This means I need to delete the line and change the axis label again and again. What is the best way to do that?

This is my current approach, but it causes problems when used in a layout:

using GLMakie

const objects = []

function plot_sin(fig)
    if length(objects) > 0
        delete!(objects[end])
    end
    ax=Axis(fig[1, 1], xlabel = "time [s]", ylabel = "sin")
    x = 0:0.1:10*pi
    y = sin.(x)
    po = lines!(x,y)   
    push!(objects, ax) 
end

function plot_cos(fig)
    if length(objects) > 0
        delete!(objects[end])
    end
    ax=Axis(fig[1, 1], xlabel = "time [s]", ylabel = "cos")
    x = 0:0.1:10*pi
    y = cos.(x)
    po = lines!(x,y)   
    push!(objects, ax) 
end

function main(gl_wait=true)
    fig=Figure()

    @async begin
        for i in 0:2
            plot_sin(fig)
            sleep(2)
            plot_cos(fig)
            sleep(2)
        end
    end

    gl_screen = display(fig)
    if gl_wait
        wait(gl_screen)
    end
    return nothing
end

main()

Any better way?

If you can get by with adjusting input positions or attributes do that. (I.e. make a node points = Node(Point2f0.(x, y)), pass it to lines and adjust that and the attributes you need.)

Otherwise you can delete! plots from the scene they’re on, i.e.

fig = Figure()
ax = Axis(fig[1, 1])
s = scatter!(ax, rand(10))
delete!(ax.scene, s)
fig

That should also be a lot faster than deleting and recreating axes.

Thank you, using a node of points works:

using GLMakie

const p1 = Node(Vector{Point2f0}(undef, 6000)) # 5 min
const y_label = Node("")

function plot_sin(x)
    y_label[]="sin"
    y = sin.(x)
    p1[] =  Point2f0.(x, y)
end

function plot_cos(x)
    y_label[]="cos"
    y = cos.(x)
    p1[] =  Point2f0.(x, y)
end

function main(gl_wait=true)
    fig=Figure()
    ax=Axis(fig[1, 1], xlabel = "time [s]", ylabel = y_label)
    x_1 = 0f0:0.1f0:10f0*pi
    x_2 = 0:0.1:5*pi
    plot_sin(x_1)
    po = lines!(p1)   

    @async begin
        for i in 0:2
            plot_sin(x_1)
            sleep(2)
            plot_cos(x_2)
            sleep(2)
        end
    end

    gl_screen = display(fig)
    if gl_wait
        wait(gl_screen)
    end
    return nothing
end

main()

But the x axis is not re-scaled when the number of points changes.

How can I achieve that?

Call autolimits!(ax) or xlims!(ax, min, max) after updating your input.