Problem with an observable array of varying length in Makie.jl

Hello.

I am facing a problem with Observables (of varying length) in Makie.jl. The problem can be summarised to the following minimal example:

using Makie
#AbstractPlotting.inline!(true) # Uncomment if using Atom and Juno.

# Made up data.
x = rand(10);
y = rand(10);

# Make x_i and y_i depend on the value of the observable i.
i = Node(1); # Initialise observable i to the value of 1.
x_i = lift(t -> x[1:t], i); # The length of x_i and y_i is determined by the value of i.
y_i = lift(t -> y[1:t], i);

scene = Scene(limits = FRect(0.0, 0.0, 2.0, 2.0));
scatter!(scene, x_i, y_i); # A scatter plot with x_i on the x axis and y_i on the y axis.
scene # See a plot with one point, as the observable i has value 1, everything is fine.
push!(i, 2); # Change value of the observable i to 2.
scene # See a plot with two points, everything is fine.
push!(i, 3); # DimensionMismatch("arrays could not be broadcast to common size") ???

Why do I get the error on the last line? To me this does not make any sense, as changing the value of i to 2 works. I noticed, that if I instead write:

x_i = lift(t -> x[1:t], i);
y_i = lift(t -> y[1:length(t)], x_i);

the error disappears. Hopefully someone can explain what is going on? I was under the impression that multiple values can depend on the same Observable.

My use case is making an animation that plots my data up to time index i.

2 Likes

Iā€™d just use an array of points, e.g. with Point2f0.(x, y)

I am interested in a similar case, with varying number of arrows instead of scatter. Is that also possible?

using Makie
# Made up data.
N = 100
pos = [rand(Point3f0) for _ in 1:N]
vel = [rand(Point3f0) for _ in 1:N]

# Make x_i and y_i depend on the value of the observable i.
i = Node(10); # Initialise observable i to the value of 1.
pos_i = lift(t -> pos[1:t], i); # The length of x_i and y_i is determined by the value of i.
vel_i = lift(t -> vel[1:t], i);

scene = Scene(limits = FRect(0.0, 0.0, 2.0, 2.0));
scene = Scene()
arrows!(scene, pos_i, vel_i)
display(scene)
push!(i, 20)

Thank you for your answer, but can you explain why exactly this happens?