Display images in real time

I have some code which updates a matrix and I would like to display it as an image which updates in real time. I first tried Plots’ @gif which produces a gif file but no live display, so I switched to Makie. This is my code. It is updating im but I can’t find a way to make it actually display the new data.

S = rand(Float64,(32,32))
fig,ax,im = image(S)
display(fig)
for i in 1:10
    sweep(S)
    im[3].val = S
    yield()
end

I’m not attached to any particular library if there is a better way of doing this.

Use Observables.jl with GLMakie.jl.

Simple example below:

using GLMakie
using Observables

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

x = Observable(randn(32, 32))
image(x)

for i = 1:10
    x[] = randn(32, 32)
    sleep(0.1)
end
1 Like

Thank you, that has got me to a working solution. I had looked at Observables but I couldn’t see how to make them work when the data is modified in place.

Working code:

S = rand(Float64,(32,32))
X = Observable(S)
image(X)

for i in 1:10
    X[]=sweep(S)
    sleep(0.1)
end

You can use notify() to trigger an update after an in-place modification:

S = rand(Float64,(32,32))
X = Observable(S)
image(X)

function sweep!(x)
  x[9:24, 9:24] = rand(Float64, (16, 16))
  nothing
end

for i in 1:10
    sweep!(S)
    notify(X)
    sleep(0.1)
end
3 Likes