# Plot can't take Observables

**URL:** <https://discourse.julialang.org/t/plot-cant-take-observables/81607>\
**Category:** New to Julia\
**Tags:** plotting, vector, glmakie\
**Created:** [May 24, 2022, 5:21pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607 "2022-05-24T17:21:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willi\_Wespe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willi_wespe/32/36558_2.png) [@Willi\_Wespe](https://discourse.julialang.org/u/Willi_Wespe)\
**Post date:** [May 24, 2022, 5:21pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/1 "2022-05-24T17:21:30Z")

</div>

I want to plot lines!() with Observables as x and y parameters.  
The value of the Obervables changes obviously, because I print them to the console.  
However, the lines!() plot won’t update:

```julia
using GLMakie

x = [[[4, 3], [-4, -3], [-0.75, 2]], [[5, 4], [-7, 8], [2, 2]], [[5, 7], [-2, 1], [9, 10]]]

xs = [Float32.(getindex.(getindex.(x,i),1)[1:end]) for i in 1:3]
ys = [Float32.(getindex.(getindex.(x,i),2)[1:end]) for i in 1:3]

x_trail = Observable(getindex.(xs, [1:1]))
y_trail = Observable(getindex.(ys, [1:1]))

f = Figure()
Axis(f[1, 1])

lines!.(x_trail, y_trail, linewidth = 3)

display(f)

for i in 1:3

    x_trail[] = getindex.(xs, [1:i])
    y_trail[] = getindex.(ys, [1:i])

    println(x_trail[])
    println(y_trail[])

    sleep(1)

end

```

Do you have any idea, how to update the plot as x\_trail and y\_trail are growing, without calling lines!() each iteration? I tried, but:

```julia
lines!.(x_trail[], y_trail[], linewidth = 3)

```

doesn’t do anything, and

```julia
lines!.(x_trail, y_trail, linewidth = 3)

```

gives the error:

ERROR: MethodError: no method matching length(::Observable{Vector{Vector{Float32}}})

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 24, 2022, 7:47pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/2 "2022-05-24T19:47:53Z")

</div>

Your input to `lines!` is nested too deeply, it needs to be `Observable{Vector{Float32}}` not `Observable{Vector{Vector{Float32}}}`  
How about this:

```julia
using GLMakie

x = [[[4, 3], [-4, -3], [-0.75, 2]], [[5, 4], [-7, 8], [2, 2]], [[5, 7], [-2, 1], [9, 10]]]

idx = Observable(1)

xs = map(idx) do i
    getindex.(x[i], 1)
end

ys = map(idx) do i
    getindex.(x[i], 2)
end

f = Figure()
Axis(f[1, 1])
limits!(-10, 10, -10, 10)

lines!(xs, ys, linewidth = 3)

for i in 1:3
    idx[] = i
    display(f)
    sleep(1)
end

```

Or, perhaps more inline with your original data structure

```julia
pts = [Point2f.(l) for l in x]

ln = map(idx) do i
    pts[i]
end

lines!(ln, linewidth=3)

```

---

<div class="post-metadata">

**Author:** ![Willi\_Wespe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willi_wespe/32/36558_2.png) [@Willi\_Wespe](https://discourse.julialang.org/u/Willi_Wespe)\
**Post date:** [May 24, 2022, 8:33pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/3 "2022-05-24T20:33:15Z")

</div>

Thanks,  
I get your idea, and the info that the input needs to be `Observable{Vector{Float32}}` is very helpful!

But my goal is to have all 3 line segments in one plot. It should look like this:

`lines!.(xs, ys, linewidth = 3)`

1. 

```julia
xs = [[4.0], [-4.0], [-0.75]]
ys = [[3.0], [-3.0], [2.0]]

```

(only points, so no line is visible)

1. 

```julia
xs = [[4.0, 5.0], [-4.0, -7.0], [-0.75, 2.0]]
ys = [[3.0, 4.0], [-3.0, 8.0], [2.0, 2.0]]

```

1. 

```julia
xs = [[4.0, 5.0, 5.0], [-4.0, -7.0, -2.0], [-0.75, 2.0, 9.0]]
ys = [[3.0, 4.0, 7.0], [-3.0, 8.0, 1.0], [2.0, 2.0, 10.0]]

```

 ![julia](https://global.discourse-cdn.com/julialang/original/3X/e/b/eb500e99ea56ac6d53efde4db5643bd43152aa6b.png)

I can produce these vectors.  
The lines! command works for these vectors, as long as I plug them in manually.  
Though when I write` lines!.(xs[], ys[])` for example which prints me the exact same vector that I plugged in manually, the program crashes

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 24, 2022, 9:23pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/4 "2022-05-24T21:23:34Z")

</div>

Ah, I see. How about this:

```julia
using GLMakie

x = [[[4, 3], [-4, -3], [-0.75, 2]], [[5, 4], [-7, 8], [2, 2]], [[5, 7], [-2, 1], [9, 10]]]
pts = [Point2f.(l) for l in x]

idx = Observable(1)
obs = [map(i->pt[1:i], idx) for pt in pts]

f = Figure()
ax = Axis(f[1,1])
limits!(-10, 10, -10, 10)

lines!.(ax, obs)

for i in 1:3
    idx[] = i
    display(f)
    sleep(1)
end

```

This create a vector of observables and broadcasts over that, broadcasting over an observable isn’t meaningful since an observable has one entry.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 25, 2022, 7:45am UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/5 "2022-05-25T07:45:55Z")

</div>

Btw, don’t call display in a loop 😉  
It’s a one off operation to create the window and OpenGL state, so it’s quite expensive.  
Also, it’s more efficient to just have one call to lines or linesegments, but maybe that’s not relevant here 😉

---

<div class="post-metadata">

**Author:** ![Willi\_Wespe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willi_wespe/32/36558_2.png) [@Willi\_Wespe](https://discourse.julialang.org/u/Willi_Wespe)\
**Post date:** [May 25, 2022, 1:23pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/6 "2022-05-25T13:23:27Z")

</div>

Thanks!

---

<div class="post-metadata">

**Author:** ![Willi\_Wespe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willi_wespe/32/36558_2.png) [@Willi\_Wespe](https://discourse.julialang.org/u/Willi_Wespe)\
**Post date:** [May 25, 2022, 1:38pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/7 "2022-05-25T13:38:52Z")

</div>

Thanks, but it’s not quite right…

See, your program produces different images than it should:

Top: my program  
Bottom: your program

 ![julia](https://global.discourse-cdn.com/julialang/original/3X/7/e/7ed8f4d86923522a59863f0144043ecbbfc74249.png)

As far as I can understand, your array `obs` gets updated each time `idx` is incremented. That causes `obs` to become an Observable itself, which again contains Observables?

However, the paramters I need for lines!.() are:

```julia
x = [[4.0, 5.0, 5.0], [-4.0, -7.0, -2.0], [-0.75, 2.0, 9.0]]
y = [[3.0, 4.0, 7.0], [-3.0, 8.0, 1.0], [2.0, 2.0, 10.0]]

```

But your program produces:

```julia
Observable{Vector{Point{2, Float32}}}[Observable{Vector{Point{2, Float32}}} with 1 listeners. Value:
Point{2, Float32}[[4.0, 3.0], [-4.0, -3.0], [-0.75, 2.0]], Observable{Vector{Point{2, Float32}}} with 1 listeners. Value:
Point{2, Float32}[[5.0, 4.0], [-7.0, 8.0], [2.0, 2.0]], Observable{Vector{Point{2, Float32}}} with 1 listeners. Value:   
Point{2, Float32}[[5.0, 7.0], [-2.0, 1.0], [9.0, 10.0]]]

```

Maybe I am asking for something that’s not possible, sorry 😅  
In this case just tell me, and I will close this question

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 25, 2022, 3:54pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/8 "2022-05-25T15:54:53Z")

</div>

Oh, oops, just a transpose

```julia
using GLMakie

x = [[[4, 3], [-4, -3], [-0.75, 2]], [[5, 4], [-7, 8], [2, 2]], [[5, 7], [-2, 1], [9, 10]]]
pts = [Point2f.(l) for l in x]
pts = reduce((a,b)->cat(a, b; dims=2), pts)

idx = Observable(1)
obs = [map(i->pts[j, 1:i], idx) for j in 1:size(pts, 1)]

f = Figure()
ax = Axis(f[1,1])
limits!(-10, 10, -10, 10)

lines!.(ax, obs)

for i in 1:3
    idx[] = i
    sleep(1)
end

```

---

<div class="post-metadata">

**Author:** ![Willi\_Wespe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/willi_wespe/32/36558_2.png) [@Willi\_Wespe](https://discourse.julialang.org/u/Willi_Wespe)\
**Post date:** [May 25, 2022, 4:09pm UTC](https://discourse.julialang.org/t/plot-cant-take-observables/81607/9 "2022-05-25T16:09:10Z")

</div>

Thank you very much! That’s exactly what I was looking for.
