Slider in scatter plot adds new layers but doesnt delete old ones

Maybe something like this :

using GLMakie, DataFrames

fig = Figure() # initialize canvas

plot = Axis(fig[1, 1]) # initialize plot

sg = SliderGrid( # create slider grid
    fig[2, 1],
    (label = "x-limit", range = 0:0.1:10, format = "{:.1f}", startvalue = 0.5),
    (label = "y-limit", range = 0:0.1:10, format = "{:.1f}", startvalue = 0.5),
    )

xlimy, ylimy = sg.sliders[1].value, sg.sliders[2].value # turn slider values into simple observable variables

df = DataFrame("xcoord" => rand(10)*10, "ycoord" => rand(10)*10) # create random dataframe

points = lift(xlimy,ylimy) do value_x,value_y
       temp_df = filter(x -> (x.xcoord >= value_x) & (x.ycoord >= value_y), df)
       [Point2f(x,y) for (x,y) in zip(temp_df.xcoord,temp_df.ycoord)]
  end

scatter!(plot,points)

If you see, scatter! is called only once but the array of points is what evolves through observables. In your case, you were plotting something new every time the sliders moved. My implementation is not very well thought so it is not the best.

A couple tangential comments:

  1. No need to import Makie, only GLMakie is needed. If you are following a tutorial where both are used you may be looking at something old.
  2. Where you do, onany, you are using both time xlimy, I think you want ylimy in one of those.
  3. You missed DataFrames in the example.
1 Like