# How to clear a plot from a scene with other elements in GLMakie

**URL:** <https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164>\
**Category:** Visualization\
**Tags:** glmakie\
**Created:** [October 10, 2024, 7:36pm UTC](https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164 "2024-10-10T19:36:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emerson\_Vitor\_Castel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emerson_vitor_castel/32/2933_2.png) [@Emerson\_Vitor\_Castel](https://discourse.julialang.org/u/Emerson_Vitor_Castel)\
**Post date:** [October 10, 2024, 7:36pm UTC](https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164/1 "2024-10-10T19:36:10Z")

</div>

Well, I’m new to using GLMakie. I wrote a script where I want to capture mouse events and plot the points using the `scatter!` function. In this same scene, I would like to insert a button with the ability to clear the points that were drawn (that is reset the scene!). I couldn’t achieve the desired result using the `empty!` command. If anyone can help, I’d greatly appreciate it. Below is the sample code.

```julia
using GLMakie
GLMakie.activate!() # hide
function gui()
     scene = Scene(camera = campixel!,size=(800,600))
     points = Observable(Point2f[])
     scatter!(scene, points, color = :gray)
     btn = Button(scene, label = "Clean")
on(btn.clicks) do _
    empty!(scene.plots)
    points = Observable(Point2f[])
    scatter!(scene, points, color = :gray)
    println(scene.plots)
    println(points)
end
on(events(scene).mousebutton) do event
    if event.button == Mouse.left
        if event.action == Mouse.press 
            mp = events(scene).mouseposition[]
            push!(points[], mp)
            notify(points)
        end
    end
end
scene
end

```

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [October 10, 2024, 7:46pm UTC](https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164/2 "2024-10-10T19:46:33Z")

</div>

Quite close, but you need `empty!(scene)`

```julia
using GLMakie
function gui()
    scene = Scene(camera=campixel!, size=(800, 600))
    points = Observable(Point2f[])
    scatter!(scene, points, color=:gray)
    btn = Button(scene, label="Clean")
    on(btn.clicks) do _
        empty!(scene)
        points = Observable(Point2f[])
        scatter!(scene, points, color=:gray)
    end
    on(events(scene).mousebutton) do event
        if event.button == Mouse.left
            if event.action == Mouse.press
                mp = events(scene).mouseposition[]
                push!(points[], mp)
                notify(points)
            end
        end
    end
    scene
end
gui()

```

---

<div class="post-metadata">

**Author:** ![Emerson\_Vitor\_Castel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emerson_vitor_castel/32/2933_2.png) [@Emerson\_Vitor\_Castel](https://discourse.julialang.org/u/Emerson_Vitor_Castel)\
**Post date:** [October 10, 2024, 7:53pm UTC](https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164/3 "2024-10-10T19:53:44Z")

</div>

> [@sdanisch](#):
>
> ```julia
> using GLMakie
> function gui()
> scene = Scene(camera=campixel!, size=(800, 600))
> points = Observable(Point2f[])
> scatter!(scene, points, color=:gray)
> btn = Button(scene, label="Clean")
> on(btn.clicks) do _
> empty!(scene)
> points = Observable(Point2f[])
> scatter!(scene, points, color=:gray)
> end
> on(events(scene).mousebutton) do event
> if event.button == Mouse.left
> if event.action == Mouse.press
> mp = events(scene).mouseposition[]
> push!(points[], mp)
> notify(points)
> end
> end
> end
> scene
> end
> gui()
> 
> ```

First of all, thank you very much for the prompt help. The problem with the proposed solution is that the entire scene will be cleared, including the button I created. I would have to recreate the button, but in this case, it seems I will encounter an error with the message: `Can only use scenes with PixelCamera as topscene`.

---

<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:** [October 11, 2024, 4:39pm UTC](https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164/4 "2024-10-11T16:39:17Z")

</div>

I think what you actually want is this. You do not need to delete your scatter, you can just update `points` with an empty array (you could also do `empty!(points[])` and then `notify(points)`).

If you really want to delete the scatter completely for some reason, it’s better to do `scat = scatter!(...)` and then `delete!(scene, scat)`.

```julia
using GLMakie
function gui()
    scene = Scene(camera=campixel!, size=(800, 600))
    points = Observable(Point2f[])
    scatter!(scene, points, color=:gray)
    btn = Button(scene, label="Clean")
    on(btn.clicks) do _
        points[] = []
    end
    on(events(scene).mousebutton) do event
        if event.button == Mouse.left
            if event.action == Mouse.press
                mp = events(scene).mouseposition[]
                push!(points[], mp)
                notify(points)
            end
        end
    end
    scene
end
gui()

```

---

<div class="post-metadata">

**Author:** ![Emerson\_Vitor\_Castel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emerson_vitor_castel/32/2933_2.png) [@Emerson\_Vitor\_Castel](https://discourse.julialang.org/u/Emerson_Vitor_Castel)\
**Post date:** [October 12, 2024, 1:09am UTC](https://discourse.julialang.org/t/how-to-clear-a-plot-from-a-scene-with-other-elements-in-glmakie/121164/5 "2024-10-12T01:09:01Z")

</div>

Perfect! That was exactly what I needed! Thank you very much!
