# Axis jitter when using slider and axis autolimits in GLMakie

**URL:** <https://discourse.julialang.org/t/axis-jitter-when-using-slider-and-axis-autolimits-in-glmakie/89687>\
**Category:** Visualization\
**Tags:** question, plotting, makie, glmakie\
**Created:** [November 3, 2022, 2:58am UTC](https://discourse.julialang.org/t/axis-jitter-when-using-slider-and-axis-autolimits-in-glmakie/89687 "2022-11-03T02:58:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garrek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrek/32/27937_2.png) [@garrek](https://discourse.julialang.org/u/garrek)\
**Post date:** [November 3, 2022, 2:58am UTC](https://discourse.julialang.org/t/axis-jitter-when-using-slider-and-axis-autolimits-in-glmakie/89687/1 "2022-11-03T02:58:28Z")

</div>

When using a Slider that changes a variable in a function, sometimes the amplitude can change quite a lot. In some cases, it makes sense to resize the axes to see what’s going on in a plot. This can easily be done in GLMakie by using `autolimits!()` after updating the variable. However, if the number of decimal places of the axis tick labels changes, this causes the Axis itself to resize, making the plot jitter. This is undesirable. I am working around this by setting `yticklabelspace` (for the y axis) large enough that this won’t happen.

I’m wondering if this is the canonical way to solve this or if there is a better way?

Example:

```julia
using GLMakie

x = 0:0.1:6π
A = Observable(1.0)
y(x, A) = A * sin.(x)

oy = Observable(y(x, to_value(A)))

fig = Figure()
display(fig)

ax = Axis(fig[1, 1], ylabel = "y", xlabel = "x",
            yticklabelspace = 20.0
            )

lines!(x, oy)

sg = SliderGrid(fig[2, 1],
    (label = "Amplitude", range = 0:0.01:10, format = "{:.1f}", startvalue=1)
)

on(sg.sliders[1].value) do val
    A[] = val
    oy[] = y(x, to_value(A))
    autolimits!(ax)
end

```

Without `yticklabelspace`  
 ![out](https://global.discourse-cdn.com/julialang/original/3X/b/0/b06cded10f9810d4ea51ccec3d2cae37264bcaf6.gif)

With `yticklabelspace` (now tick labels cover the axis label, but that can be fixed)  
 ![out_on](https://global.discourse-cdn.com/julialang/original/3X/4/1/414eeabd5b98cc946989ab78aa82c75cbb5a464f.gif)

---

<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:** [November 3, 2022, 10:36am UTC](https://discourse.julialang.org/t/axis-jitter-when-using-slider-and-axis-autolimits-in-glmakie/89687/2 "2022-11-03T10:36:21Z")

</div>

During normal axis zooming with the mouse, this reset action is delayed because it would be annoying if the axis constantly jittered under the cursor. You can do something similar I think. Currently, the slider doesn’t make it easy to access the event when the drag stops (it will after this PR [Add option to update when releasing mouse button from slide by lwhitefox · Pull Request #2228 · MakieOrg/Makie.jl · GitHub](https://github.com/MakieOrg/Makie.jl/pull/2228)) but you could do this in pseudocode:

```julia
tight_ticklabels!(ax) # set good fixed limits to start with

timer = SomeTimer(latency = 300ms_or_so)

on(timer_fires) do
    tight_ticklabels!(ax)
end

on(slider.value) do value
    update_plot_using(value)
    start_or_reset_some_timer!(timer)
end

```

---

<div class="post-metadata">

**Author:** ![garrek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrek/32/27937_2.png) [@garrek](https://discourse.julialang.org/u/garrek)\
**Post date:** [November 6, 2022, 6:26am UTC](https://discourse.julialang.org/t/axis-jitter-when-using-slider-and-axis-autolimits-in-glmakie/89687/3 "2022-11-06T06:26:31Z")

</div>

Thanks for the suggestion. This works alright, but getting the timer right is tricky. It would be nice if it would update only when the slider stopped. The callback function only runs once while I’m moving the slider and I’m not sure how to get the timer to fire with each value change of the slider. I’m not sure if I’m implementing this right, but this is what I’m trying:

```julia
x = 0:0.1:6π
A = Observable(1.0)
y(x, A) = A * sin.(x)

oy = Observable(y(x, to_value(A)))

fig = Figure()
display(fig)
ax = Axis(fig[1, 1], ylabel = "y", xlabel = "x")
lines!(x, oy)

tight_ticklabel_spacing!(ax)

sg = SliderGrid(fig[2, 1],
    (label = "Amplitude", range = 0:0.01:10, format = "{:.1f}", startvalue=1))

function cb(timer)
    wait(timer)
    tight_ticklabel_spacing!(ax)
end

on(sg.sliders[1].value) do val
    A[] = val
    oy[] = y(x, to_value(A))
    autolimits!(ax)
    timer = Timer(cb, 0.3, interval=0.3)
end

```
