How to create a string with observable values in GLMakie?

Hello,
I am using GLMakie to make an interactive plot. I would like to make a label to a slider that shows the actual value of an observable. But how do I extract the value so that it can be rounded and displayed?
In the example below, I have tried with different combinations, but I always get an error (the third print something bu the value never changes).
What is the right syntax?
Thank you

using GLMakie
# starting parameters
x = 0.1
y = 1.6
z = 1.9
p = [x,y,x]
# ranges
x_vals = LinRange(0,10, 10)
y_vals = LinRange(-2,5, 20)
z_vals = LinRange(-10,10, 100)
# Observable
p_mod = Observable(p)
# plot
fig = Figure()
ax = Axis(fig[1,1:2])
# sliders
Label(fig[2,1], string("X value:", round(p_mod[1], digits=2)))
x_sld = Slider(fig[2,2], range=x_vals, startvalue=x)
Label(fig[3,1], string("Y value:", round(y_sld.value, digits=2)))
y_sld = Slider(fig[3,2], range=x_vals, startvalue=x)
Label(fig[4,1], string("Z value:", round(p_mod.val[3], digits=2)))
z_sld = Slider(fig[4,2], range=x_vals, startvalue=x)
# change values
on(x_sld.value) do val 
    p_mod.val[1] = val 
    p_mod[] = p_mod[]  
end
on(y_sld.value) do val 
    p_mod.val[2] = val 
    p_mod[] = p_mod[]  
end
on(z_sld.value) do val 
    p_mod.val[3] = val 
    p_mod[] = p_mod[]  
end

Try Label(fig[2,1], @lift string("X value:", round($p_mod[1], digits=2)))

Thank you!

Also check SliderGrid which has some good default logic for labeled sliders. Also works for a single one.

https://docs.makie.org/stable/reference/blocks/slidergrid/

1 Like

One more thing: is it possible to increase the font size of the label? I tried with:

julia> Label(fig[2,1], @lift string("X value:", round($p_mod[1], digits=2)), fontsize = 30)
ERROR: syntax: ""X value:"" is not a valid function argument name around /home/gigiux/Documents/Model/testMakie.jl:17
Stacktrace:
 [1] top-level scope
   @ ~/Documents/Model/testMakie.jl:17

julia> Label(fig[2,1], @lift string("X value:", round($p_mod[1], digits=2), fontsize = 30))
ERROR: MethodError: no method matching string(::String, ::Float64; fontsize::Int64)

Closest candidates are:
  string(::Any...) got unsupported keyword argument "fontsize"
   @ Base strings/io.jl:189
  string(::String) got unsupported keyword argument "fontsize"
   @ Base strings/substring.jl:181
  string(::Union{Char, String, Symbol}...) got unsupported keyword argument "fontsize"
   @ Base strings/substring.jl:225
  ...

Stacktrace:
 [1] (::var"#9#10")(arg1#228::Vector{Float64})
   @ Main ./none:0
 [2] #map#13
   @ ~/.julia/packages/Observables/YdEbO/src/Observables.jl:570 [inlined]
 [3] map(::var"#9#10", ::Observable{Vector{Float64}})
   @ Observables ~/.julia/packages/Observables/YdEbO/src/Observables.jl:568
 [4] top-level scope
   @ ~/Documents/Model/testMakie.jl:17

Try with @lift(string("X value:", round($p_mod[1], digits=2))) so that the @lift macro doesn’t take the fontsize as argument.

Also to make the code more readable you can write it on two lines. That also solves the fontsize issue:

xvalue = @lift string("X value:", round($p_mod[1], digits=2))
Label(fig[2,1], xvalue, fontsize=30)
1 Like

Thank you!