Dynamical label text using @lift (makie)

Hi, I am trying to simulate a football game with Makie. Thereby the score and the time are updated in each step of the animstep!() function. I do not get an error message, but neither the time nor the result is updated.

using GLMakie
using Makie
using FileIO
using CairoMakie

t = Observable("0:00")
goals_x = Observable(0)
goals_y = Observable(0)

times = game_time() #array with strings from "0:00" to "90:00"

fig = Figure(resolution=(600, 600),backgroundcolor = "skyblue")
axes = Axis(fig[1,0], aspect = DataAspect(), yreversed = false, title = "Ecuador")
    hidedecorations!(axes)
    hidespines!(axes)
    tightlimits!.(axes)

axes1 = Axis(fig[1,3], aspect = DataAspect(), yreversed = false, title = "Qatar")
    hidedecorations!(axes1)
    hidespines!(axes1)
    tightlimits!.(axes1)

text = @lift("$($t)")
result = @lift("$($goals_x):$(($goals_y))")

image!(axes, img)
image!(axes1, img1)

Label(fig[1, :], result, textsize = 50)
Label(fig[0, :], text, textsize = 20)

display(fig)

for i in 1:(90*60 + 1)
    animstep!("A1","A2",teams,i,t,goals_x,goals_y,times)
    #sleep(0.001)
end

The animstep!() is defined as follows:

function animstep!(x,y,teams,i,t,goals_x,goals_y,times)
    t[] = times[i]
    goals_x[] += game_step("A1","A2",teams,i)[1]
    goals_y[] += game_step("A1","A2",teams,i)[2]
end

About a tip or any form of help I would be very grateful :slight_smile:

I was able to update a Label successfully using this small example

using GLMakie

time = Observable(0.0)
dt = 0.1

text = @lift("time = $($time)")

fig = Figure()
ax = Axis(fig[1, 1])
Label(fig[1, 1], text)

step!() = time[] += dt

for i in 1:100
    step!()
    sleep(0.01)
end

Try to reduce your code to a MWE (Minimum Working Example) so I could take a look and run it to see what is failing if you still have not managed to solve the issue.

Thanks for your answer! I think your code is a good example of a MWE for my code. One difference is that I still have display(fig) after the line “Label(fig[1, 1], text)”. If I don’t add display(fig), then when I run your code in my notebook, no figure is output. Maybe this is the problem ?

I used this example to create my code: Making animated and interactive scientific visualizations in Makie.jl · GitHub.

You do need display(fig) if you are working on a Jupyter Notebook, for example.

But, even when using a Jupyter notebook, executing the following code inside a single cell works for me.

using GLMakie

time = Observable(0.0)
dt = 0.1

text = @lift("time = $($time)")

fig = Figure()
ax = Axis(fig[1, 1])
Label(fig[1, 1], text)

step!() = time[] += dt

display(fig)

for i in 1:100
    step!()
    sleep(0.01)
end

What are you using to execute your code?

1 Like

Hi, I finally found my error. Apparently the output of my cernel was blocked because the port was unreachable due to the machine’s firewall security. After I changed the port everything works again.

I work with Jupyter Notebook (1.8.0). I had overlooked that in the command prompt the error " zmq message arrived on closed channel" was returned .

Thanks for your help! Without your example of error free code, I would never have found my error.

1 Like